繁体   English   中英

打印字典中的元素时,打印命令中的 0 索引是什么?

[英]what is the 0 index in the print command when printing elements in a dictionary?

我试过这段代码,但我不明白为什么我们在 {0[a]} 中使用 0 索引。

Day={'a':'Saturday','b':'Sunday','c':'Monday',
      'd':'Tuesday','e':'Wednesday','f':'Thursday','g':'Friday'}

print('the first days is {0[a]} , second days is {0[b]}'.format(Day))

0是指传递给format(...) function 的第一个参数。 1是第二个,以此类推。

或者,您可以将名称 arguments 传递给format ,并在字符串中使用它们的名称。

0指的是str.format方法的第一个参数。
因此,您的 print 相当于

print('the first days is {0} , second days is {1}'.format(Day['a'], Day['b']))

或者

print(f"the first days is {Day['a']} , second days is {Day['b']}")

0不是用于索引字典,而是用于获取.format(..)括号中的第一项。

例子:

>>> '{0} {1}'.format('Hello', 'World')
'Hello World'
>>> 

暂无
暂无

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

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