繁体   English   中英

python 元组仅打印键名

[英]python tuple print key name only

有人可以给我一个关于如何只打印元组中所有键的名称的提示吗? IE,

拉尔夫马利山姆

例如,我将如何只打印下面每个动物的名称而不是嵌套键(如果我的术语正确......)列出什么类型的动物和标识符..

animals = {'ralph': ('dog', 160101),
        'marley': ('dog', 160102),
        'sam': ('cat', 160103),
        'bones': ('dog', 160104),
        'bella': ('cat', 160105),
        'max': ('dog', 160106),
        'daisy': ('cat', 160107),
        'angel': ('cat', 160108),
        'luna': ('cat', 160109),
        'buddy': ('dog', 160110),
        'coco': ('dog', 160111),
       }

#dict(TUPLE)[key]
d = dict(animals)


for animal in animals.items():
    print(animal)

不完全确定你在尝试什么。 将它们全部计算在一个 go 中。

>>> for name, animal in animals.items():
...     animal_type, the_number = animal
...     print(f'The {animal_type} {name} has the number {the_number}')
...
The dog ralph has the number 160101
The dog marley has the number 160102
The cat sam has the number 160103
The dog bones has the number 160104
The cat bella has the number 160105
The dog max has the number 160106
The cat daisy has the number 160107
The cat angel has the number 160108
The cat luna has the number 160109
The dog buddy has the number 160110
The dog coco has the number 160111
>>>

这里有两件重要的事情:

  1. dict.items()从字典中产生键和值
  2. a, b = c将元组c = (0, 0) “解包”为名称ab ,如a = c[0]b = c[1] (元组必须恰好有两个值)

还值得一提的是,如果您需要字典键(而不是值或两者), .keys()产生字典中的每个键。 .values()做同样的事情,但对于值。

有人可以给我一个关于如何只打印元组中所有键的名称的提示吗?

你的animals变量是一个dict ,而不是一个tuple 它的是元组。 所以你的问题实际上是“我如何打印字典的键”,这很简单:

print(animals.keys())

或者如果你想要每行一个:

print("n".join(animals)

或者如果你真的想要一个 for 循环:

for key in animals: 
    print(key)

请注意,我们在最后两个示例中没有使用.keys()方法,因为dict是可迭代的,它们在键上进行迭代,而不是值。

您可能想阅读精美的手册(从官方教程开始)以了解有关“术语”(确实非常重要)以及标准数据类型必须提供的更多信息。

这应该这样做。

for animal in animals.keys():
    print(animal)

你期待这个吗? animals.items()中的每次迭代都包含两个不同的值,键和元组。 您应该通过在循环中添加 position [0]来解决第一个索引中的键。

for animal in animals.items():
    print(animal[0])

Output:

ralph
marley
sam
bones
bella
max
daisy
angel
luna
buddy
coco
for key, value in animals.items():
    print(key)

首先,你的animals元组实际上已经是一个字典,而不是一个元组。 所以在它上面调用dict()是多余的。

此外,您的 dict animals的条目是键/值对的组合,其中动物的名称作为键,它们的物种和编号作为实际元组中的各自值。

您使用的dict.items()返回一个元组列表(key, value) ,您可以像索引列表一样对其进行索引。 所以以下应该工作:

animals = {'ralph': ('dog', 160101),
            'marley': ('dog', 160102),
            'sam': ('cat', 160103),
            'bones': ('dog', 160104),
            'bella': ('cat', 160105),
            'max': ('dog', 160106),
            'daisy': ('cat', 160107),
            'angel': ('cat', 160108),
            'luna': ('cat', 160109),
            'buddy': ('dog', 160110),
            'coco': ('dog', 160111),
           }

for KeyAndValue in animals.items():
    print(KeyAndValue[0])

结果是:

ralph
marley
sam
bones
bella
max
daisy
angel
luna
buddy
coco

您也可以通过以下方式仅迭代键:

for name in animals.keys():
    print(name)

结果是:

ralph
marley
sam
bones
bella
max
daisy
angel
luna
buddy
coco

请注意,您实际上不必调用dict.keys()方法,因为字典默认情况下可以通过其键进行迭代。 有关详细信息和示例,请参阅bruno desthuilliers 答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM