我有这份清单,是对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
创建气泡排序,该气泡排序可以根据子数组的索引对嵌套数组进行排序
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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。