【发布时间】:2020-05-12 21:47:56
【问题描述】:
我有这样的字典dict,以元组为键:
dict = { (1, 1): 10, (2,1): 12}
并尝试像这样访问它:
new_dict = {}
for key, value in dict:
new_dict["A"] = key[0]
new_dict["B"] = key[1]
new_dict["C"] = value
但它失败了,因为key 似乎没有解析为元组。正确的方法是什么?
【问题讨论】:
-
如果你使用 Python 3,你可以试试
for key, value in dict.items():
标签: python dictionary