我现在正在处理 Python 字典。我写了一段代码:
import random
categories = {1 : "Antics", 2 : "Tickets", 3: "Moviez",
4 : "Music", 5 : "Photography", 6 : "Gamez", 7 : "Bookz",
8 : "Jewelry", 9 : "Computers", 10 : "Clothes"}
items = {"Picture" : 1, "Clock" : 1, "Ticket for Mettalica concert" : 2,
"Ticket for Iron Maiden concert" : 2, "Ticket for Placebo concert" : 2,
"The pianist" : 3, "Batman" : 3, "Spider-Man" : 3,
"WoW" : 6, "Cabal" : 6, "Diablo 3" : 6, "Diablo 2" : 6,
"Thinking in Java" : 7, "Thinking in C++" : 7, "Golden ring" : 8,
"Asus" : 10, "HP" : 10, "Shoes" : 11}
for key, val in categories :
for k, v in items :
if key == v :
print(val, k)
我想创建一个第三个字典,在那里我会喜欢:
dictThe3rd = {"Antics" : "Picture", "Antics" : "Clock", "Tickets" : "Ticket for Mettalica concert", "Ticket" : "Ticket for Iron Maiden concert", "Ticket" : "Ticket for Placebo concert", ...}
等等。
这该怎么做?我的代码显示:
test.py, line 14, in <module> for key, val in categories : TypeError: 'int' object is not iterable