简体   繁体   English

如何枚举以下列表?

[英]How do I enumerate the following list?

I am trying to enumerate the following list but the output I get is not correct:我正在尝试枚举以下列表,但我得到的 output 不正确:

colours = ["red", "green", "blue", "yellow"]
for p, colours in enumerate(colours): 
    print(p, ("-->"), colours[p])

The output I get is:我得到的 output 是:

0 --> r
1 --> r
2 --> u
3 --> l

which is not what I am after of course, any help?这当然不是我所追求的,有什么帮助吗? Thanks!谢谢!

colours = ["red", "green", "blue", "yellow"]
for p, colour in enumerate(colours): 
    print (f"{p} --> {colour}")

1st: don't use same name for element of a loop as a variable you are iterating.第一:不要将循环元素与您正在迭代的变量使用相同的名称。 2nd: what output are you expecting?第二:你期待什么 output? Single line when printing?打印时单行? This would be done in my update.这将在我的更新中完成。 It puts variables in f-string.它将变量放在 f-string 中。

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

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