我想检查嵌套列表中是否有 in/not

I want to check if something is in/not in a nested list

希望大家帮忙看看代码

sheros=[['Groot',23,34,35],['Rocket',45,56,67]]

print(sheros)
newname=input('what is your name? ' )
newstat=int(input('what is the new stat? ' ))

listlength=len(sheros)

while listlength!=0:
    if sheros[listlength-1][0]==newname:
        sheros[listlength-1][3]=sheros[listlength-1][2]
        sheros[listlength-1][2]=sheros[listlength-1][1]
        sheros[listlength-1][1]=newstat
        listlength-=1
    else:
        listlength-=1

#this is the part doing my head in!
if newname in sheros:
    print('already here')
else:
    sheros+=[[newname,newstat,0,0]]
print('added')

print(sheros)

我想要它,这样如果指定的人已经在列表中,它会将他们的新统计数据添加到 3 的列表中并推出最旧的统计数据(在 while 循环中排序)但我想添加嵌套列表末尾的人,如果他们还没有出现(新条目),请看#this is the part doing my head in!

来不及了吗,我杜松子酒喝多了吗?

脑袋疼的请帮帮我

罗斯

#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
    sheros = [['Groot',23,34,35],['Rocket',45,56,67]]

    print(sheros)
    newname = input('what is your name? ' )
    newstat = int(input('what is the new stat? ' ))

    found = False  # Use a flag to track whether name was found
    for entry in sheros:  # Don't use counter. Just loop over elements.
        if entry[0] == newname:
            entry[2:4] = entry[1:3]  # Use list slicing
            entry[1] = newstat
            found = True
            break  # Exit early assuming names are unique.

    if found:
        print("Already here")
    else:
        sheros.append([newname, newstat, 0, 0])

    print(sheros)


if __name__ == "__main__":
    main()

做一个像this.It这样的函数可能对你有帮助。

def is_elem(l, e):
    return any(e==i[0] for i in l)

例子

In [2]: sheros=[['Groot',23,34,35],['Rocket',45,56,67]]

In [3]: is_elem(sheros, 'Groot')
Out[3]: True

In [4]: is_elem(sheros, 'Other')
Out[4]: False