我正在嘗試洗掉串列串列中的整個索引(在本例中為 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("===========================================================")
我不確定為什么它不起作用。list_name.append(item)
請注意,除了remove_player 函式之外,我不允許使用任何串列函式。
謝謝!
uj5u.com熱心網友回復:
你能試試這個嗎?
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')
uj5u.com熱心網友回復:
您可以嘗試使用將洗掉特定索引的函式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.")
這里的檔案
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.)
uj5u.com熱心網友回復:
您的代碼除了一些細節之外作業正常。你永遠不會改變原來的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
,您將在列印時看到它有效。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/490567.html