我需要創建這個函式,sort_by_chips(player_list). 這就是它的目的:
This function takes the list of players (list of lists) and returns a copy of the list in descending order of chip balance. Where two players have the same chip balance, the player with the lower number of games played value should appear first. This function must NOT modify the player list being passed in as a parameter. Your solution must NOT make use of List methods or any of the sort functions available in the Python Standard Library. This function returns a copy of the player list (list of lists) sorted in descending order of chip balance. You may wish to create/define additional functions (which are called from within this function) to assist with this task.
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]]
這就是我想要的:
new_list = [['Gina Linetti', 7, 4, 0, 3, 300, 15], ['Bruce Wayne', 5, 5, 0, 0, 100, 15], ['Buster Bluth', 3, 0, 2, 1, 50, 1]], ['Johnny Rose', 6, 2, 0, 4, 20, 10], ['Jessica Jones', 12, 0, 6, 6, 10, 6]]
這就是我到目前為止所擁有的功能。
def sort_by_chips(player_list):
largest_chip = 0
highest_chip_list = []
for player in player_list:
if player[5] > largest_chip:
largest_chip = player[5]
for i in range(len(player_list)):
games_played = player_list[i][1]
amount_of_chip = player_list[i][5]
if amount_of_chip >= largest_chip:
highest_chip_list.append(player_list[i])
print(highest_chip_list)
return highest_chip_list
但是,當我希望它從降序排列時,這只回傳最高的串列索引,而不是串列中的其他任何東西。我不確定如何讓它遍歷我的整個串列。請注意,我不能使用任何串列方法或排序函式,我需要回傳串列的副本,就像我已經完成的那樣。
謝謝!
uj5u.com熱心網友回復:
有很多演算法可供整理,對你來說一個很好的練習是探索其中的一些并比較它們的優缺點(性能、效率、簡單性等)。
這是您的問題的一個作業示例:
def swapPositions(list, pos1, pos2):
tmp = list[pos1]
list[pos1] = list[pos2]
list[pos2] = tmp
def sort_by_chips(player_list):
newlist = player_list
print("Starting list: ",newlist)
for i in range(1, len(player_list)):
for j in range(0, i):
if newlist[i][5] > newlist[j][5]:
swapPositions(newlist, i,j)
print("Ending list: ",newlist)
return newlist
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]]
sort_by_chips(player_list=player_list)
uj5u.com熱心網友回復:
請在您的問題中指定籌碼索引。假設索引是 5,我試著寫一段代碼。它不是一個函式,但可以完成作業并且可能會提供另一種觀點:
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]]
chip_list = [player[5] for player in player_list]
idx_list = []
for index, chip in enumerate(chip_list):
i_num = len(player_list)-1
for index2, chip2 in enumerate(chip_list):
if chip > chip2:
i_num = i_num-1
idx_list.append(i_num)
final_list = [0 for i in range(len(player_list)) ]
for i,idx in enumerate(idx_list):
final_list[idx] = player_list[i]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/490550.html
上一篇:使用Orc.Controls.Wizard和Catel的向導
下一篇:python資料框組合串列的列
