[英]printing a single value of a list with enumerate function
I have the following statement:我有以下声明:
for i, char in enumerate(list(range(100))):对于 i, char in enumerate(list(range(100))):
now i just want to print the value, where char is equal to 50.现在我只想打印值,其中 char 等于 50。
I have tried the following idea, but did not understand why it is incorrect:我尝试了以下想法,但不明白为什么它不正确:
for i, char in enumerate(list(range(100))):
char == 50
print(i)
The solution is that I have to put an "if" statement, i already understood the logic behind it but also want to ask, why my approach is not valid.解决方案是我必须写一个“if”语句,我已经理解了它背后的逻辑,但也想问一下,为什么我的方法无效。
Thanks for helping in advance.感谢您提前提供帮助。
You're almost there, you just have to use the if
condition like so:你快到了,你只需要像这样使用
if
条件:
for i, char in enumerate(list(range(100))):
if char == 50: #<--- add if here
print(i) #<--- inside the if-condition
Also, I see there is no need to use enumerate
or list
.另外,我认为没有必要使用
enumerate
或list
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.