简体   繁体   English

尝试在python中排序2d数组以输出高分列表

[英]Trying to sort 2d array in python to output a list of highscores

So I am making a game server thing that lets users login and choose a game to play and then adds whatever score they got to a notepad file in the format: (ie blackjack) username, #chips, "chips". 因此,我正在制作一个游戏服务器,允许用户登录并选择要玩的游戏,然后将他们获得的任何分数添加到记事本文件中,格式为:(即二十一点)用户名,#chips,“ chips”。 Now i want to sort through all the scores in the file to show on my gui but I'm not allowed to use built in functions like sorted but rather have to come up with an algorithm. 现在,我想对文件中的所有分数进行排序以显示在gui上,但是我不允许使用诸如sorted之类的内置函数,而必须提出一种算法。 I've tried a bubble sort and insertion sort without any luck. 我尝试了冒泡排序和插入排序,但没有任何运气。 Here's what I have so far: 这是我到目前为止的内容:

blackjackList = [['harsh', '4', 'chips'], ['ahmed', '25', 'chips'], ['yousef', '1003', 'chips'], ['krushangi', '200', 'chips'], ['bombberman', '1202', 'chips']]
def bubbleSort(alist):
    for passnum in range(len(alist)-1,0,-1):
        for i in range(passnum):
            if alist[i][1]>alist[i+1][1]:
                temp = alist[i]
                alist[i] = alist[i+1]
                alist[i+1] = temp
        return alist

This currently outputs 当前输出

[['ahmed', '25', 'chips'], ['yousef', '1003', 'chips'], ['krushangi', '200', 'chips'], ['bombberman', '1202', 'chips'], ['harsh', '4', 'chips']]

when I use it with blackjacklist but i want it to make blackjacklist go in order from ascending to descending scores. 当我将其与21点列表一起使用时,我希望它使21点列表按升序从降序排列。

Your problem is that the return statement is indented too much and sits inside the for loop therefore it only does one pass through before returning. 您的问题是return语句缩进过多,并且位于for循环内,因此它仅在返回之前进行一次遍历。

Also, you are comparing the numbers as strings. 另外,您正在将数字作为字符串进行比较。 This will not give the expected order (eg "10" is less than "2"). 这不会给出预期的顺序(例如“ 10”小于“ 2”)。 You should use int() to convert them to integers which will then sort correctly. 您应该使用int()将其转换为整数,然后将其正确排序。

You can use quicksort to sort the arrays. 您可以使用quicksort对数组进行排序。

>>> blackjackList = [['harsh', '4', 'chips'], ['ahmed', '25', 'chips'], ['yousef', '1003', 'chips'], ['krushangi', '200', 'chips'], ['bombberman', '1202', 'chips']]
>>> def quicksort(arr):
...     if len(arr)==0: return []
...     if len(arr)==1: return arr
...     left = [i for i in arr[1:] if int(i[1])<int(arr[0][1])]    # for descending, exchange
...     right = [i for i in arr[1:] if int(i[1])>=int(arr[0][1])]  # these two values
...     return quicksort(left)+[arr[0]]+quicksort(right)
...
>>> quicksort(blackjackList)
[['harsh', '4', 'chips'], ['ahmed', '25', 'chips'], ['krushangi', '200', 'chips'], ['yousef', '1003', 'chips'], ['bombberman', '1202', 'chips']]

The above code gives the ascending sorted list. 上面的代码给出了升序的排序列表。 To get descending sorted list, just exchange the values for left and right in the code. 要获得降序排序列表,只需在代码中交换leftright的值即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM