我想在元组字典中访问元组的特定元素。 假设每个键都有一个带有唯一键的字典和一个带有三个值的元组。 我想编写一个迭代器,为字典中的每个元素打印一个元组中的每三个项目。
例如
dict = {"abc":(1,2,3), "bcd":(2,3,4), "cde", (3,4,5)} for item in dict: print item[2]但这会回来
c d e我哪里错了?
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.
For example
dict = {"abc":(1,2,3), "bcd":(2,3,4), "cde", (3,4,5)} for item in dict: print item[2]But this returns
c d eWhere am I going wrong?
最满意答案
for item in dict: print dict[item][2]此外,您不应该在内置后命名任何内容,因此请将字典命名为'd'或'dict'之外'dict'其他内容
for item in dict:中的for item in dict.keys()相同。
或者,你可以这样做:
for item in dict.values(): print item[2] for item in dict: print dict[item][2]Also, you should not name anything after a built-in, so name your dictionary 'd' or something other than 'dict'
for item in dict: does the same thing as for item in dict.keys().
Alternatively, you can do:
for item in dict.values(): print item[2]更多推荐
发布评论