简体   繁体   English

Python 打印字典中的值的值

[英]Python printing values of values in a dictionary

In Python, I know you print the values of a key in a dictionary by doing variable.values() .在 Python 中,我知道您通过执行variable.values()来打印字典中键的值。 But what if I wanted to print a value of a particular value?但是如果我想打印一个特定值的值怎么办?

For an example:例如:

pets = {'Dog': ['Poodle', 'Boxer', 'Terrier'], 'Cat': ['Sphynx', 'Ragdoll', 'Birman']}

I want to specifically choose a particular breed of animal.我想专门选择一种特定的动物品种。 For instance, I just want to print Boxer .例如,我只想打印Boxer

You could do something like:您可以执行以下操作:

animal = list(pets.values())[0]
breed = list(animal.values())[1]
print(breed)

This will give you Boxer .这会给你Boxer However, I was curious if I can do this in a single line of code like printing a value of a value.但是,我很好奇我是否可以在一行代码中做到这一点,比如打印一个值的值。

I've tried:我试过了:

breed = list(list(pets.values()))[0][1]

but it tells me that the string index is out of range.但它告诉我字符串索引超出范围。

print(list(pets.values()))

output: output:

[['Poodle', 'Boxer', 'Terrier'], ['Sphynx', 'Ragdoll', 'Birman']]
  • As you can see its a list of list.如您所见,它是一个列表列表。 Now each item is list you can access by indexing.现在每个项目都是您可以通过索引访问的列表。
breed = list(pets.values())[0][1]
  • this will give you desired result.这会给你想要的结果。

your data structure is a dict with "sets" as values.您的数据结构是一个以“集合”为值的字典。 sets are not subscriptable and converting in a list doesn't keep the order集合不可下标,并且在列表中转换不会保持顺序

>>> list(pets['Dog'])
['Boxer', 'Poodle', 'Terrier']

the question is then.... what do you really need?那么问题是......你真正需要什么?

If you need to access to breed you could/should use lists or tuples instead of sets如果您需要访问品种,您可以/应该使用列表或元组而不是集合

it seems strange about how you are using dict, I assume in your case you should use list instead您如何使用 dict 似乎很奇怪,我假设在您的情况下您应该使用 list 代替

pets = {'Animal': {'Dog': ['Poodle', 'Boxer', 'Terrier'], 'Cat': ['Sphynx', 'Ragdoll', 'Birman']}}

if you want a specific breed to print, then you might need an additional key for that to gain benefits of using dict如果您想要打印特定品种,那么您可能需要一个额外的密钥才能获得使用 dict 的好处

pets = {'Animal': {'Dog': {1:'Poodle', 2:'Boxer', 3:'Terrier'}, 'Cat': ['Sphynx', 'Ragdoll', 'Birman']}}

then you can get boxer with然后你可以得到拳击手

pets['Animal']['Dog'][2]

The way you are defining this dictionary creates a syntax error:您定义此字典的方式会产生语法错误:

>>> pets = {'Animal': 'Dog':{'Poodle','Boxer','Terrier'}, 'Cat':{'Sphynx','Ragdoll','Birman'}}
  File "<stdin>", line 1
    pets = {'Animal': 'Dog':{'Poodle','Boxer','Terrier'}, 'Cat':{'Sphynx','Ragdoll','Birman'}}
                           ^
SyntaxError: invalid syntax

I created a similar dictionary (this time a dictionary of species as key and breeds as values), and show how you can index this new dictionary:我创建了一个类似的字典(这次是一个以物种为键,以品种为值的字典),并展示了如何索引这个新字典:

>>> pets = {'Dog':('Poodle','Boxer','Terrier'), 'Cat':('Sphynx','Ragdoll','Birman')}
>>> pets['Dog'][1]
'Boxer'
>>>

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

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