在列表中,根据索引值执行任务?
好的,所以我一直在尝试获取列表“bean”中的特定索引,以根据该索引的值执行任务。
下面是代码:
def dude():
beans = ['639', '939']
bun = 0
heyo_beans = beans[bun]
while bun in range(len(beans)):
while bun in range(len(beans)):
try:
b = heyo_beans.index('639')
except ValueError:
print("Do Nothing")
break
else:
print("Do something with variable b")
break
bun = bun + 1
print(bun)
print(beans[1])
dude()
输出:
Do something with variable b
1
Do something with variable b
2
939
我应该得到:
Do something with variable b
1
Do Nothing
2
939
“Do Nothing”应该出现在第二次循环中,因为索引更改为索引 2,即 939。 最终输出939是为了验证bean索引1确实是939。 有什么想法吗?
另一个例子是:
list = ['111', '222', '333']
#goes through the list in a while loop, each time adding +1 index, for range in length
if list index = 111:
"Perform task A"
elif list index = 222:
"Perform task B"
elif list index = 333:
"Perform task C"
else:
"Do Nothing"
list index = index + 1
关于如何进行这项工作有什么建议吗?
这也不起作用:
def dude():
list_to_beans = ['639', '939']
bun = 0
heyo_beans = list_to_beans[bun]
while bun in range(len(list_to_beans)):
for bun in list_to_beans:
if heyo_beans == '639':
print("Do something")
else:
print("Do nothing")
bun = bun + 1
print(bun)
dude()
输出:
Do something
Do something
应该说:
Do something
Do nothing
2
Ok, so I have been trying to get specific indexes in my list "beans" to perform a task depending on the value of that index.
Here is the code:
def dude():
beans = ['639', '939']
bun = 0
heyo_beans = beans[bun]
while bun in range(len(beans)):
while bun in range(len(beans)):
try:
b = heyo_beans.index('639')
except ValueError:
print("Do Nothing")
break
else:
print("Do something with variable b")
break
bun = bun + 1
print(bun)
print(beans[1])
dude()
OUTPUT:
Do something with variable b
1
Do something with variable b
2
939
I should be getting:
Do something with variable b
1
Do Nothing
2
939
"Do Nothing" should appear the second go around as the index changed to index 2, which is 939.
The final output of 939 is to verify that bean index 1 is indeed 939.
Any ideas?
Another example would be:
list = ['111', '222', '333']
#goes through the list in a while loop, each time adding +1 index, for range in length
if list index = 111:
"Perform task A"
elif list index = 222:
"Perform task B"
elif list index = 333:
"Perform task C"
else:
"Do Nothing"
list index = index + 1
Any suggestions on how to make this work?
This also does not work:
def dude():
list_to_beans = ['639', '939']
bun = 0
heyo_beans = list_to_beans[bun]
while bun in range(len(list_to_beans)):
for bun in list_to_beans:
if heyo_beans == '639':
print("Do something")
else:
print("Do nothing")
bun = bun + 1
print(bun)
dude()
OUTPUT:
Do something
Do something
Should say:
Do something
Do nothing
2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案:
输出:
Answer:
OUTPUT:
你让这种方式变得比需要的更加复杂。只需使用普通的
for
循环即可迭代列表的所有元素。您将索引与元素混淆了。由于列表包含字符串,因此您必须与字符串进行比较,而不是与数字进行比较。
You're making this way more complicated than it needs to be. Just use an ordinary
for
loop to iterate over all the elements of the list. You're confusing indexes with the elements.Since the list contains strings, you have to compare with strings, not numbers.