请帮助修复 ValueError: ... is not in the list python

问题描述 投票:0回答:0

我有以下代码:

def negative_nbr(x):
    ls = []
    for i in x:
        if i < 0:
            ls += [ls.index(i)]
    return ls
y = [1,-5, 3, -4, 3]
negative_nbr(y)

结果:

>>>
ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_1452\3255678202.py in <module>
      1 y = [1,-5, 3, -4, 3]
----> 2 negative_nbr(y)

~\AppData\Local\Temp\ipykernel_1452\3262551839.py in negative_nbr(x)
      3     for i in x:
      4         if i < 0:
----> 5             ls += [ls.index(i)]
      6     return ls

ValueError: -5 is not in list

请帮忙解决 非常感谢

enter image description here帮助

python list indexing naming
© www.soinside.com 2019 - 2024. All rights reserved.