如何在Python中从列表的列表中删除索引?



我试图删除列表列表(本例中为John)中的整个索引,如这里所示,列表称为player_list:

player_list = [['Bruce Wayne', 5, 5, 0, 0, 100, 15], 
['Jessica Jones', 12, 0, 6, 6, 10, 6], 
['Johnny Rose', 6, 2, 0, 4, 20, 10], 
['Gina Linetti', 7, 4, 0, 3, 300, 15], 
['Buster Bluth', 3, 0, 2, 1, 50, 1], 
['John', 0, 0, 0, 0, 100, 0]]

然而,当我运行函数remove_player:

def remove_player(player_list, name):
check = find_player(player_list, name)
new_list = []
if check == -1:
print(name, "is not found in players")
else:
for i in range(len(player_list)):
if player_list[i] != player_list[check]:
new_list.append(player_list[i])
player_list = new_list

print(name, "has sucessfully been removed.")

和功能:find_player:

def find_player(player_list, name):
found = -1
index = 0
while index < len(player_list) and found == -1:
if name == player_list[index][0]:
found = index
index += 1
return found

name来源:

removed_player = input("Please enter name: ")
remove = remove_player(player_list, removed_player)

它确实从列表中删除了索引,但是它仍然打印在我在这里创建的表中,这不是我想要它做的:

===========================================================
-                     Player Summary                      -
===========================================================
-                            P  W  L  D    Chips    Score -
===========================================================
- Bruce Wayne                5  5  0  0      100       15 -
-----------------------------------------------------------
- Jessica Jones             12  0  6  6       10        6 -
-----------------------------------------------------------
- Johnny Rose                6  2  0  4       20       10 -
-----------------------------------------------------------
- Gina Linetti               7  4  0  3      300       15 -
-----------------------------------------------------------
- Buster Bluth               3  0  2  1       50        1 -
-----------------------------------------------------------
- John                       0  0  0  0      100        0 -
-----------------------------------------------------------
===========================================================

我为表display_players(player_list)创建的函数:

def display_players(player_list):
print("===========================================================")
print("-", format("Player Summary", ">34s"), format("-", ">22s"))
print("===========================================================")
print("-", format("P", ">28s"), format("W", ">2s"), format("L", ">2s"), format("D", ">2s"), format("Chips", ">8s"), format("Score", ">8s"), format("-", ">1s"))
print("===========================================================")
for i in range(len(player_list)):
print("-", format(str(player_list[i][0]), "<25s"), format(str(player_list[i][1]), ">2s"), format(str(player_list[i][2]), ">2s"), format(str(player_list[i][3]), ">2s"), format(str(player_list[i][4]), ">2s"), format(str(player_list[i][5]), ">8s"), format(str(player_list[i][6]), ">8s"), ("-"))
print("-----------------------------------------------------------")
print("===========================================================")

我不知道为什么它不工作。请注意,我不允许在remove_player函数中使用list_name.append(item)以外的任何列表函数。

谢谢!

你能试一下吗?

player_list = [['Bruce Wayne', 5, 5, 0, 0, 100, 15], 
['Jessica Jones', 12, 0, 6, 6, 10, 6], 
['Johnny Rose', 6, 2, 0, 4, 20, 10], 
['Gina Linetti', 7, 4, 0, 3, 300, 15], 
['Buster Bluth', 3, 0, 2, 1, 50, 1], 
['John', 0, 0, 0, 0, 100, 0]]
def remove_player(player_list, name):
return [ player for player in player_list if player[0] != name]
player_list = remove_player(player_list, 'Bruce Wayne')

您可以尝试使用pop函数来删除特定的索引。

check = find_player(player_list, name)
if check == -1:
print(name, "is not found in players")
else:
player_list.pop(check)
print(name, "has sucessfully been removed.")

这里是doc

list.pop([i])
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)

您的代码除了一些小细节外工作良好。你永远不能改变原来的player_list。在函数中使用它来生成new_list,然后将player_list赋值给它,但所有这些都在函数内部,您不会返回它以在函数外部访问它。

下面是remove_player函数:
def remove_player(player_list, name):
check = find_player(player_list, name)
new_list = []
if check == -1:
print(name, "is not found in players")
else:
for i in range(len(player_list)):
if player_list[i] != player_list[check]:
new_list.append(player_list[i])
player_list = new_list

print(name, "has sucessfully been removed.")
return player_list

我只在末尾添加了return语句。

现在当调用这个时:

removed_player = input("Please enter name: ")
player_list = remove_player(player_list, removed_player)

将返回的列表赋值给player_list,您将看到,当打印它时,它工作了。

最新更新