简体   繁体   English

“列表索引超出范围”仍然出现错误

[英]Still getting error on “list index out of range”

basically i'm trying to find whether or not a list has the '007' in them.基本上我正在尝试查找列表中是否包含“007”。 the output still spits out what i'm looking for but at the end i'm still getting "list index out of range" error. output 仍然吐出我正在寻找的内容,但最后我仍然收到“列表索引超出范围”错误。 C&P jupyter and see for yourself. C&P jupyter 自己看看吧。 i know it has something to do with the huge ass if statement but i just dont see the issue, i'm basically just looking for '007' which if it doesn't it'd just skip its logic... is it my code or is it jupyter acting crazy?我知道这与巨大的 ass if 语句有关,但我只是没有看到这个问题,我基本上只是在寻找“007”,如果不是,它就会跳过它的逻辑......是我的吗?代码还是 jupyter 疯了?

def spy_game(nums):
    listy = []

    for x in nums:
        if x == 0 or x == 7:
            listy.append(x)

    print(listy)
    #compare listy indexes
    for x, y in enumerate(listy):
        if y == 0 and listy[x + 1] == 0 and listy[x + 2] == 7:
            print(True)
            break
        elif y == 7:
            print(False)

spy_game([1, 2, 4, 0, 0, 7, 5])
spy_game([1, 0, 2, 4, 0, 5, 7])
spy_game([1, 7, 2, 0, 4, 5, 0])
L = [1, 2, 4, 0, 0, 7, 5]

if "007" in "".join([str(x) for x in L]):
     # There is a 007 in the list

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

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