简体   繁体   English

列表中的 Python 无限循环

[英]Python Infinite Loop on List

I'm sure it's something very obvious that I am missing but can someone tell me why this code snippet continues in an endless loop?我确定我遗漏了一些非常明显的东西,但有人能告诉我为什么这段代码片段会无限循环吗?

bucket = [['Louis', 29], ['Nick', 2], ['Rochelle', 4]]

def find_index(bucket,keyword):
    index = 0
    for e in bucket:
        while e[0] <> keyword:
            index = index + 1
    return index


print find_index(bucket,'Nick')

How about this ?这个怎么样 ?
Two for-loops to prevent the loop running infinite.两个 for 循环以防止循环无限运行。

bucket = [['Louis', 29], ['Nick', 2], ['Rochelle', 4]]

def find_index(bucket, keyword):
    for index, subArray in enumerate(bucket):
        for element in subArray:
            if element == keyword:
                return index

print find_index(bucket, 'Nick')

How about?怎么样?

bucket = [['Louis', 29], ['Nick', 2], ['Rochelle', 4]]

def find_index(bucket,keyword):
    index = 0
    for e in bucket:
        while e[index] <> keyword:
            index = index + 1
    return index


print find_index(bucket,'Nick')

You have used e[0] instead of e[index] .您使用了e[0]而不是e[index]

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

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