开发者社区> 问答> 正文

如何使用python对2D数组进行冒泡排序

我有这份清单,是对2018年一些NHL球员数据的摘要。我想按pts对其进行排序,这是使用泡沫的第7个值。我知道python上的内置排序功能,但是我宁愿使用冒泡排序,甚至快速排序。有人可以帮忙吗?

[['A.J. Greer', 'COL', 'LW', '15', '1', '1', '2', '14', '9', '20', '5'], ['Aaron Ekblad', 'FLA', 'D', '82', '13', '24', '37', '47', '180', '114', '88'], ['Adam Clendening', 'CLS', 'D', '4', '0', '0', '0', '0', '3', '1', '3'], ['Adam Cracknell', 'FA', 'C', '2', '0', '0', '0', '0', '3', '6', '0'], ['Adam Erne', 'DET', 'LW', '65', '7', '13', '20', '40', '70', '159', '26'], ['Adam Gaudette', 'VAN', 'C', '56', '5', '7', '12', '18', '55', '48', '15']]

这是我到目前为止所做的:

def sortByPoints(stats):

    lengthOfstats = len(stats) - 1
    for i in range(lengthOfstats):
        for j in range(lengthOfstats - i):
            if stats[j] < stats[j + 1]:
                stats[j], stats[j + 1] = stats[j + 1], stats[j]

    return stats

print(sortByPoints(readStatsFromFile()))

问题来源:stackoverflow

展开
收起
is大龙 2020-03-23 18:56:25 544 0
1 条回答
写回答
取消 提交回答
  • 创建气泡排序,该气泡排序可以根据子数组的索引对嵌套数组进行排序

    BubbleSort的修改

    def bubbleSort(arr, ind = 6):
        """Bubble sort arr based upon subelement ind (default of index 6) 
           which is 7th element of sub-array since 0 based indexing"""
        n = len(arr)
    
        # Traverse through all array elements
        for i in range(n):
    
            # Last i elements are already in place
            for j in range(0, n-i-1):
    
                # traverse the array from 0 to n-i-1
                # Swap if the element found is greater
                # than the next element
                if int(arr[j][ind]) > int(arr[j+1][ind]) :
                    arr[j], arr[j+1] = arr[j+1], arr[j]
        return arr
    

    测试

    arr = [['A.J. Greer', 'COL', 'LW', '15', '1', '1', '2', '14', '9', '20', '5'], ['Aaron Ekblad', 'FLA', 'D', '82', '13', '24', '37', '47', '180', '114', '88'], ['Adam Clendening', 'CLS', 'D', '4', '0', '0', '0', '0', '3', '1', '3'], ['Adam Cracknell', 'FA', 'C', '2', '0', '0', '0', '0', '3', '6', '0'], ['Adam Erne', 'DET', 'LW', '65', '7', '13', '20', '40', '70', '159', '26'], ['Adam Gaudette', 'VAN', 'C', '56', '5', '7', '12', '18', '55', '48', '15']]
    
    import pprint
    print('pre-sorted')
    pprint.pprint(arr)
    print('sorted')
    pprint.pprint(bubbleSort(arr))
    

    输出

     pre-sorted
    [['A.J. Greer', 'COL', 'LW', '15', '1', '1', '2', '14', '9', '20', '5'], 
     ['Aaron Ekblad', 'FLA', 'D', '82', '13', '24', '37', '47', '180', '114', '88'], 
     ['Adam Clendening', 'CLS', 'D', '4', '0', '0', '0', '0', '3', '1', '3'], 
     ['Adam Cracknell', 'FA', 'C', '2', '0', '0', '0', '0', '3', '6', '0'], 
     ['Adam Erne', 'DET', 'LW', '65', '7', '13', '20', '40', '70', '159', '26'], 
     ['Adam Gaudette', 'VAN', 'C', '56', '5', '7', '12', '18', '55', '48', '15']]
    
    sorted
    [['Adam Clendening', 'CLS', 'D', '4', '0', '0', '0', '0', '3', '1', '3'], 
     ['Adam Cracknell', 'FA', 'C', '2', '0', '0', '0', '0', '3', '6', '0'], 
     ['A.J. Greer', 'COL', 'LW', '15', '1', '1', '2', '14', '9', '20', '5'], 
     ['Adam Gaudette', 'VAN', 'C', '56', '5', '7', '12', '18', '55', '48', '15'], 
     ['Adam Erne', 'DET', 'LW', '65', '7', '13', '20', '40', '70', '159', '26'], 
     ['Aaron Ekblad', 'FLA', 'D', '82', '13', '24', '37', '47', '180', '114', '88']]
    

    回答来源:stackoverflow

    2020-03-23 18:56:35
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载