如何访问元组字典的特定元素 [英] How to access specific element of dictionary of tuples
本文介绍了如何访问元组字典的特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想访问元组字典中元组的特定元素.假设我有一个具有唯一键的字典,以及每个键具有三个值的元组.我想编写一个迭代器,为字典中的每个元素打印元组中的每三个项目.
I want to access a specific element of a tuple in a dictionary of tuples. Let's say that I have a dictionary with a unique key, and a tuple with three values, for each key. I want to write a iterator the prints every third item in a tuple for every element in the dictionary.
例如
dict = {"abc":(1,2,3), "bcd":(2,3,4), "cde", (3,4,5)}
for item in dict:
print item[2]
但这又回来了
c
d
e
我要去哪里错了?
推荐答案
for item in dict:
print dict[item][2]
此外,您不应以内置名称命名,因此应将字典命名为'd'
或'dict'
Also, you should not name anything after a built-in, so name your dictionary 'd'
or something other than 'dict'
for item in dict:
与for item in dict.keys()
的作用相同.
或者,您可以执行以下操作:
Alternatively, you can do:
for item in dict.values():
print item[2]
这篇关于如何访问元组字典的特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文