简体   繁体   English

如何从 1 开始迭代字典项和索引?

[英]How to iterate over dictionary items and indexes starting from 1?


这是图像

I want to make it like this: (sorted number '1-3' instead of '0-2')我想让它像这样:(排序数字'1-3'而不是'0-2')

  1. name John名字约翰
  2. age 20 20岁
  3. gender Male性别 男

You can use enumerate with its start argument, it's literally made for this:您可以将enumerate与它的start参数一起使用,它实际上是为此而设计的:

for i, (key, value) in enumerate(my_lib.items(), start=1):
    print(i, key, value)

Try this:尝试这个:

for i, (x, y) in enumerate(my_lib.items()):
    print(i+1, x, y)

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

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