如何访问元组字典的特定元素(How to access specific element of dictionary of tuples)

系统教程 行业动态 更新时间:2024-06-14 16:59:17
如何访问元组字典的特定元素(How to access specific element of dictionary of tuples)

我想在元组字典中访问元组的特定元素。 假设每个键都有一个带有唯一键的字典和一个带有三个值的元组。 我想编写一个迭代器,为字典中的每个元素打印一个元组中的每三个项目。

例如

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 e

Where 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]

更多推荐

本文发布于:2023-04-16 14:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/59f0bc0b11b59e8e660eb80aa2787c01.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字典   元素   access   specific   dictionary

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!