2017-08-12 84 views
0

但是我想從最小到最大排序幾個點。 我都希望得到這樣的結果從最小到最大排序

Drogba 2 pts 
Owen 4 pts 
Henry 6 pts 

然而,我的排名卻似乎恰恰相反,現在:-(

Henry 6 pts 
Owen 4 pts 
Drogba 2 pts 

我想我的問題是我的功能冒泡

def Bubblesort(name, goal1, point): 
    swap = True 
    while swap: 
     swap = False 
     for i in range(len(name)-1): 
      if goal1[i+1] > goal1[i]: 
       goal1[i], goal1[i+1] = goal1[i+1], goal1[i] 
       name[i], name[i+1] = name[i+1], name[i] 
       point[i], point[i + 1] = point[i + 1], point[i] 
       swap = True 
    return name, goal1, point 

def ranking(name, point): 
    for i in range(len(name)): 
    print(name[i], "\t" , point[i], " \t ") 

name = ["Henry", "Owen", "Drogba"] 
point = [0]*3 
goal1 = [68, 52, 46] 
gain = [6,4,2] 


name, goal1, point = Bubblesort(name, goal1, point ) 
for i in range(len(name)): 
    point[i] += gain[i] 

ranking (name, point) 
+0

如果目標1 [I + 1]>目標1 [I]: - 讓它 '<' – 2017-08-12 14:13:29

+0

總是同樣的問題:-(@ - 阿卜杜拉Alemadi – tamzoro

回答

0

在您的代碼:

  if goal1[i+1] > goal1[i]: 

檢查它是否更大。如果下一個更小,則不需要更換。

修改成:

  if goal1[i+1] < goal1[i]: 
+0

總是同樣的問題:-(@ - 奧利弗鎳 – tamzoro

+0

沒問題!如果這對你有幫助,請按回答旁邊的複選標記接受它! –

0

一堆問題:

  • def Bubblesort - PEP8說,函數名應該是小寫,即def bubblesort

  • 你存儲你的數據,一堆平行列表;這使得工作和思考(以及排序)變得更加困難。你應該調換你的數據,以便不要有名單列表,點數列表,目標列表你有一個球員列表,每個球員都有一個名字,積分,目標。

  • def bubblesort(name, goal1, point): - 應該像def bubblesort(items)因爲bubblesort並不需要知道它是越來越名稱和目標,並分和目標(專業這種方式讓你從後重新使用功能,其他的事情排序)排序。所有它需要知道的是,它正在獲取項目列表,並且它可以使用>比較項目對,即定義了Item.__gt__

  • 不使用默認的「原生」的排序順序,Python的排序函數通常讓你通過一個可選的key功能,它允許你告訴它作爲排序依據是什麼 - 那就是,排序上的key(items[i]) > key(items[j])代替items[i] > items[j]。這通常比重組數據以獲得您想要的排序順序更有效率和/或方便。

  • for i in range(len(name)-1): - 您正在迭代超過需要。每次通過後,剩餘列表中的最高值被推到頂部(因此「泡泡」排序,值像氣泡一樣上升到列表頂部)。您不需要再次查看這些最高值,因爲您已經知道它們高於其餘任何值;在第n次通過後,您可以忽略最後的n個值。

  • 實際上,情況比這好一點;您經常會發現已按排序順序排列的值的運行。如果你跟蹤實際交換的最高指數,你不需要在下一次通過時超越這個指標。

所以,你的排序功能變得

def bubblesort(items, *, key=None): 
    """ 
    Return items in sorted order 
    """ 
    # work on a copy of the list (don't destroy the original) 
    items = list(items) 
    # process key values - cache the result of key(item) 
    # so it doesn't have to be called repeatedly 
    keys = items if key is None else [key(item) for item in items] 
    # initialize the "last item to sort on the next pass" index 
    last_swap = len(items) - 1 
    # sort! 
    while last_swap: 
     ls = 0 
     for i in range(last_swap): 
      j = i + 1 
      if keys[i] > keys[j]: 
       # have to swap keys and items at the same time, 
       # because keys may be an alias for items 
       items[i], items[j], keys[i], keys[j] = items[j], items[i], keys[j], keys[i] 
       # made a swap - update the last_swap index 
       ls = i 
     last_swap = ls 
    return items 

你可能不相信,這實際上是正確的,讓我們來測試一下:

from random import sample 

def test_bubblesort(tries = 1000): 
    # example key function 
    key_fn = lambda item: (item[2], item[0], item[1]) 

    for i in range(tries): 
     # create some sample data to sort 
     data = [sample("abcdefghijk", 3) for j in range(10)] 
     # no-key sort 
     assert bubblesort(data) == sorted(data), "Error: bubblesort({}) gives {}".format(data, bubblesort(data)) 
     # keyed sort 
     assert bubblesort(data, key=key_fn) == sorted(data, key=key_fn), "Error: bubblesort({}, key) gives {}".format(data, bubblesort(data, key_fn)) 

test_bubblesort() 

現在你的代碼的其餘部分變成

class Player: 
    def __init__(self, name, points, goals, gains): 
     self.name = name 
     self.points = points 
     self.goals = goals 
     self.gains = gains 

players = [ 
    Player("Henry", 0, 68, 6), 
    Player("Owen", 0, 52, 4), 
    Player("Drogba", 0, 46, 2) 
] 

# sort by goals 
players = bubblesort(players, key = lambda player: player.goals) 

# update points 
for player in players: 
    player.points += player.gains 

# show the result 
for player in players: 
    print("{player.name:<10s} {player.points:>2d} pts".format(player=player)) 

which which produce

Drogba  2 pts 
Owen  4 pts 
Henry  6 pts