简体   繁体   English

如何打印字典中的多个项目

[英]How to print multiple items from dictionary

I have a VERY basic question here, so please don't laugh.我在这里有一个非常基本的问题,所以请不要笑。 I can't find any specific resolution to this, and the manual I am using does not address this question specifically, probably because it is something really obvious.我找不到任何具体的解决方案,我使用的手册也没有具体解决这个问题,可能是因为它确实很明显。 When using:使用时:

print residents['tenantA']打印居民['tenantA']

I get the correct output " 36 " from the dictionary我从字典中得到正确的 output “ 36

residents = {'tenantA': 36, 'tenantB': 37, 'tenantC': 38}居民 = {'tenantA': 36, 'tenantB': 37, 'tenantC': 38}

But when I try this with more than one item from the dictionary as follows:但是当我尝试使用字典中的多个项目时,如下所示:

print residents['tenantB', 'tenantC']打印居民['tenantB', 'tenantC']

I get the following error:我收到以下错误:

KeyError: ('tenantB', 'tenantC') KeyError: ('tenantB', 'tenantC')

I have also tried我也试过

print residents['tenantB' + 'tenantC']打印居民['tenantB' + 'tenantC']

but as expected, this just concatenates tenantB and tenantC to tenantBtenantC .但正如预期的那样,这只是将tenantBtenantC连接到tenantBtenantC

I have also tried:我也试过:

print residents[1:]打印居民 [1:]

but then I get the error但后来我得到了错误

TypeError: unhashable type类型错误:不可散列的类型

The error message does not tell me much.错误消息并没有告诉我太多。 Could anyone let me know what I am missing here?谁能让我知道我在这里缺少什么?

Thanks.谢谢。

print residents['tenantB', 'tenantC']

This tells python to look for a key called 'tenantB', 'tenantC' , and you don't have that key in your dict.这告诉 python 寻找名为'tenantB', 'tenantC'键,而您的字典中没有该键。

Probably you wanted this instead:可能你想要这个:

print residents['tenantB'], residents['tenantC']

Python converts 'tenantB', 'tenantC' as a tuple and looks for tuple as a key for the dict. Python 将 'tenantB'、'tenantC' 转换为元组,并查找元组作为字典的键。 Which is evident from the error message,从错误消息中可以明显看出,

KeyError: ('tenantB', 'tenantC')

You can see that the key is printed as tuple ('tenantB', 'tenantC') .您可以看到密钥打印为元组('tenantB', 'tenantC')

Hence individual keys must be printed separately, like因此,必须单独打印各个键,例如

print residents['tenantB'], residents['tenantC']

In python3:在 python3 中:

print(residents['tenantB'], residents['tenantC'])

Found this page as I was struggling with the same issue for an online course I am doing.找到这个页面是因为我正在为我正在做的在线课程努力解决同样的问题。 I'm a newbie of sorts and not been able to find anything so far in Google that indicates it's possible to pack multiple items in a print statement.我是一个新手,到目前为止在谷歌中还没有找到任何表明可以在打印语句中打包多个项目的东西。

So the resolution I've worked out is as follows (am sure there is a better way, but not found it yet):所以我制定的解决方案如下(我确信有更好的方法,但还没有找到):

A1 = dictionary_name[entry No.]['key-1']
A2 = dictionary_name[entry No.]['key-2']
A3 = dictionary_name[entry No.]['key-3']

print(f"Compare A: {A1}, {A2}, {A3}")  

The variables A1, A2 & A3 are assigned with the values of each key and print as this output:变量 A1、A2 和 A3 分配有每个键的值并打印为 output:

Compare A: National Geographic, Magazine, United States比较 A:国家地理,杂志,美国

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

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