我目前在 python 中有 9 个数组,我根据分数分别对它们进行冒泡排序。我怎样才能让它变小? (使用数组记录)
I currently have 9 arrays in python which i am bubblesorting separately depending on the score. how can I make it smaller? (using a record of arrays)
我当前的代码如下,如有任何帮助,我们将不胜感激。这目前工作得很好,但它在很多代码行中完成了工作。目前有 9 个数组,我希望它们只是数组的记录。我不知道该怎么做。请 post 举个例子。
placement = [1, 2 ,3 ,4 ,5]
grade = ["a", "c", "e", "d", "b"]
score = [50, 20, 13, 21, 31]
music = ["song1", "song2", "song3", "song4", "song5"]
maxcombo = [1, 2 ,3 ,4 ,5]
perfect = [1, 2 ,3 ,4 ,5]
great = [1, 2 ,3 ,4 ,5]
good = [1, 2 ,3 ,4 ,5]
miss = [1, 2 ,3 ,4 ,5]
for passnum in range(len(score)-1,0,-1):
for i in range(passnum):
if score[i]<score[i+1]:
TemporaryScore = score[i] #stores score
TemporaryGrade = grade[i] #stores grade
TemporaryMusic = music[i] #stores music
Temporarymaxcombo = maxcombo[i] #stores maxcombo
Temporaryperfect = perfect[i] #stores perfect
Temporarygreat = great[i] #stores great
Temporarygood = good[i] #stores good
Temporarymiss = miss[i] #stores miss
score[i] = score[i+1] #swaps the score
grade[i] = grade[i+1] #swaps grade
music[i] = music[i+1] #swaps music
maxcombo[i] = maxcombo[i+1] #swaps maxcombo
perfect[i] = perfect[i+1] #swaps perfect
great[i] = perfect[i+1] #swaps perfect
good[i] = good[i+1] #swaps good
miss[i] = miss[i+1] #swaps miss
score[i+1] = TemporaryScore #stores score
grade[i+1] = TemporaryGrade #stores grade
music[i+1] = TemporaryMusic #stores music
maxcombo[i+1] = Temporarymaxcombo #stores maxcombo
perfect[i+1] = Temporaryperfect #stores perfect
great[i+1] = Temporarygreat #stores great
good[i+1] = Temporarygood #stores good
miss[i+1] = Temporarymiss #stores miss
看看this question。
因此,将其与数组上的 for 循环相结合,您需要:
for array in [score, grade, music, maxcombo, perfect, great, good, miss]:
array[i], array[i+1] = array[i+1], array[i]
好的数据结构可以让你把必要的代码压缩到几乎没有,看这个简化的例子:
import operator
data = [
{"grade": "c", "score": 20},
{"grade": "e", "score": 13},
{"grade": "a", "score": 50},
]
print sorted(data, key=operator.itemgetter("score"), reverse=True)
我当前的代码如下,如有任何帮助,我们将不胜感激。这目前工作得很好,但它在很多代码行中完成了工作。目前有 9 个数组,我希望它们只是数组的记录。我不知道该怎么做。请 post 举个例子。
placement = [1, 2 ,3 ,4 ,5]
grade = ["a", "c", "e", "d", "b"]
score = [50, 20, 13, 21, 31]
music = ["song1", "song2", "song3", "song4", "song5"]
maxcombo = [1, 2 ,3 ,4 ,5]
perfect = [1, 2 ,3 ,4 ,5]
great = [1, 2 ,3 ,4 ,5]
good = [1, 2 ,3 ,4 ,5]
miss = [1, 2 ,3 ,4 ,5]
for passnum in range(len(score)-1,0,-1):
for i in range(passnum):
if score[i]<score[i+1]:
TemporaryScore = score[i] #stores score
TemporaryGrade = grade[i] #stores grade
TemporaryMusic = music[i] #stores music
Temporarymaxcombo = maxcombo[i] #stores maxcombo
Temporaryperfect = perfect[i] #stores perfect
Temporarygreat = great[i] #stores great
Temporarygood = good[i] #stores good
Temporarymiss = miss[i] #stores miss
score[i] = score[i+1] #swaps the score
grade[i] = grade[i+1] #swaps grade
music[i] = music[i+1] #swaps music
maxcombo[i] = maxcombo[i+1] #swaps maxcombo
perfect[i] = perfect[i+1] #swaps perfect
great[i] = perfect[i+1] #swaps perfect
good[i] = good[i+1] #swaps good
miss[i] = miss[i+1] #swaps miss
score[i+1] = TemporaryScore #stores score
grade[i+1] = TemporaryGrade #stores grade
music[i+1] = TemporaryMusic #stores music
maxcombo[i+1] = Temporarymaxcombo #stores maxcombo
perfect[i+1] = Temporaryperfect #stores perfect
great[i+1] = Temporarygreat #stores great
good[i+1] = Temporarygood #stores good
miss[i+1] = Temporarymiss #stores miss
看看this question。
因此,将其与数组上的 for 循环相结合,您需要:
for array in [score, grade, music, maxcombo, perfect, great, good, miss]:
array[i], array[i+1] = array[i+1], array[i]
好的数据结构可以让你把必要的代码压缩到几乎没有,看这个简化的例子:
import operator
data = [
{"grade": "c", "score": 20},
{"grade": "e", "score": 13},
{"grade": "a", "score": 50},
]
print sorted(data, key=operator.itemgetter("score"), reverse=True)