小编Bar*_*onk的帖子

以元组为键访问字典的键值

我有dict这样的字典,以元组为键:

 dict = { (1, 1): 10, (2,1): 12} 
Run Code Online (Sandbox Code Playgroud)

并尝试像这样访问它:

new_dict = {}
for key, value in dict: 
    new_dict["A"] = key[0]
    new_dict["B"] = key[1]
    new_dict["C"] = value
Run Code Online (Sandbox Code Playgroud)

但它失败了,因为key似乎没有解析为元组。正确的方法是什么?

python dictionary

2
推荐指数
1
解决办法
3367
查看次数

标签 统计

dictionary ×1

python ×1