在列表中,根据索引值执行任务?

发布于 2025-01-11 09:26:42 字数 1708 浏览 0 评论 0原文

好的,所以我一直在尝试获取列表“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽袖吟 2025-01-18 09:26:43

答案:

def dude():
    list_to_beans = ['639', '939']
    bun = 0
    heyo_beans = list_to_beans[bun]
       
    for heyo_beans in list_to_beans:
        if heyo_beans == '639':
            print("Do something")
        else:
            print("Do nothing")
    bun = bun + 1
    print(bun)

    
dude()

输出:

Do something
Do Nothing
1

Answer:

def dude():
    list_to_beans = ['639', '939']
    bun = 0
    heyo_beans = list_to_beans[bun]
       
    for heyo_beans 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 Nothing
1
表情可笑 2025-01-18 09:26:42

你让这种方式变得比需要的更加复杂。只需使用普通的 for 循环即可迭代列表的所有元素。您将索引与元素混淆了。

由于列表包含字符串,因此您必须与字符串进行比较,而不是与数字进行比较。

mylist = ['111', '222', '333']
for item in mylist:
    if item == '111':
        "Perform task A"
    elif item == '222':
        "Perform task B"
    elif item == '333':
        "Perform task C"
    else:
        "Do Nothing"

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.

mylist = ['111', '222', '333']
for item in mylist:
    if item == '111':
        "Perform task A"
    elif item == '222':
        "Perform task B"
    elif item == '333':
        "Perform task C"
    else:
        "Do Nothing"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文