简体   繁体   English

如何打印字典中键为整数的元素?

[英]how to print a element of a dictionary whose key is integral?

I am a newer to python, i have learned print format like: %[(dic-name)][flags][width][.precision]format and i want to practice it but i have a problem and i want to know why.thanks我是 python 的新手,我已经学习了打印格式,如: %[(dic-name)][flags][width][.precision]format我想练习它但我有一个问题,我想知道为什么。谢谢

My Python version is:我的Python版本是:

Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information

and i use IPython我使用 IPython

 IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

I have a dictionary like this:我有一本这样的字典:

dic1 = {1:"hello",2:"python"}

i want to print a element of the dictionary with print function.我想用打印功能打印字典的一个元素。

print("The first element is %(1)s"%dic1)

But i got a KeyError like this:但是我得到了这样的 KeyError:

  ---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-3-3712b45a538a> in <module>
----> 1 print("The first element is %(1)s"%dic1)

KeyError: '1'

If the dictionary is:如果字典是:

dic2 = {"1":"hello","2":"python"}

i will get the correct result.我会得到正确的结果。 why the dic1 can not print like that and what's the right format?为什么 dic1 不能像那样打印,正确的格式是什么?

You can usually use dictionary keys with star unpacking您通常可以使用带有星形解包的字典键

dic1 = {"key1":"hello","key2":"python"}
print("{key1}".format(**dic1))

Here the keys are numbers (and not even strings but it doesn't matter anyway: to use the technique above, the arguments must be valid as python identifiers, see what are valid keys according to Python str.format() documentation )这里的键是数字(甚至不是字符串,但无论如何都无所谓:要使用上述技术,参数必须作为 python 标识符有效,根据 Python str.format() 文档查看什么是有效键

When doing print("{1}".format(whatever)) , format interprets them as positional argument (2nd argument) and it fails miserably.在执行print("{1}".format(whatever))format将它们解释为位置参数(第二个参数)并且它失败了。

One solution is to pass the dictionary to format and query it from the format string:一种解决方案是将字典传递给format并从格式字符串中查询:

dic1 = {1:"hello",2:"python"}

print("{dic[1]}".format(dic=dic1))

result:结果:

hello

The problem with your line is that you are passing a '1' (string), not a 1 (int)您的线路的问题在于您传递的是“1”(字符串),而不是 1(整数)

Note : the new format() method isthe recommended one for python 3.x.注意:新的format()方法是推荐用于 python 3.x 的方法。

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

print("The first element is {d[1]}".format(d=dic1))

If you want to order your items by number then use list instead如果您想按编号订购商品,请改用列表

Also you can do this:你也可以这样做:

dict([(1, 2), (3, 4)])

The result will be结果将是

{1: 2, 3: 4}

The key in your dictionary is a integer, and the key in the print function is parse as map are string.字典中的键是一个整数,打印函数中的键是解析为 map 是字符串。 That give you this error.那给你这个错误。 You can print in this ways:您可以通过以下方式打印:

 print("The first element is ",dic1[1])
 print("The first element is {}".format(dic1[1]))

also you can change the keys in dict to string: dic1 = {"1":"hello","2":"python"}您也可以将 dict 中的键更改为字符串: dic1 = {"1":"hello","2":"python"}

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

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