del x[n] 不工作

del x[n] not working

我有一个列表,我正在尝试删除其中的第一项

我试过使用这段代码,但没有任何作用

del rows[rows.index(userInfo)][9].split()[0]

上下文

rows[rows.index(userInfo)][9].split() 

在列表中找到一个是字符串的项目然后将字符串拆分成一个列表,这个字符串是

Skyfall Skyfall DarkKnight DieHard  CaptainAmerica Deadpool TheMatrix CaptainAmerica TheMatrix  CaptainAmerica TheBourneIdentity

尝试以下方法

lst = rows[rows.index(userInfo)][9].split()
del lst[0]

当您在一行中执行此操作时,不会保存 split() 创建的列表,因此从匿名列表中删除元素没有实际效果。如果您先将列表分配给变量,则可以使用 del 删除第一个元素,然后根据需要对列表进行操作。

我认为问题在于,您尝试从非持久性列表中删除某些内容。
....split() returns 您尝试从中删除元素的列表。但是这个列表没有存储。
您至少需要两行:

mylist = ....split()
del mylist[0]

由于split()不能就地工作,所以无论如何你都需要存储它... 所以你也可以这样写:

mylist = foo.split()[1:]

您可以使用 pop() 或像这样通过切片对其进行索引:

rows[rows.index(userInfo)][9] = " ".join(rows[rows.index(userInfo)][9].split().pop())

rows[rows.index(userInfo)][9] = " ".join(rows[rows.index(userInfo)][9].split()[1:])