1

my current code is below, any help is appreciated. This currently works well but it does the job in so many lines of code. there is currently 9 arrays and I would like them to just be a record of arrays. I do not know how to go about doing this. Please post examples of going about this.

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
Daniel
  • 11
  • 3
  • 8
  • Fun fact: python does tuple assignment correctly, i.e. `score[i], score[i+1] = score[i+1], score[i]` will do what you expect it to. That ought to cut down on your code a bit. – Mostly Harmless Jun 03 '15 at 03:49

2 Answers2

0

Take a look at this question.

So, combining this with a for loop over your arrays, you want:

for array in [score, grade, music, maxcombo, perfect, great, good, miss]:
    array[i], array[i+1] = array[i+1], array[i]
Community
  • 1
  • 1
kponz
  • 508
  • 3
  • 7
0

Good data structure allows you to collapse the necessary code to almost nothing, see this simplified example:

import operator

data = [
    {"grade": "c", "score": 20},
    {"grade": "e", "score": 13},
    {"grade": "a", "score": 50},
]

print sorted(data, key=operator.itemgetter("score"), reverse=True)
dlask
  • 8,776
  • 1
  • 26
  • 30
  • how would I then go about saving to it. for example from my main module the grade would be "B" and the score would be "40" how would i append it to data? – Daniel Jun 03 '15 at 04:22
  • `data.append({"grade": "b", "score": 40})` – dlask Jun 03 '15 at 04:25
  • Is there any other means of contacting you? You have been really helpful :) – Daniel Jun 03 '15 at 04:28
  • You are open to different solutions and that's very important. – dlask Jun 03 '15 at 04:34
  • Sorry to bother you so much but I am planning on printing this information using PyGame. How would I separate Grade and Score so that it can be displayed correctly? – Daniel Jun 03 '15 at 04:38
  • `grades = [d["grade"] for d in data]` – dlask Jun 03 '15 at 05:59
  • hey I have a really massive issue with this, for some reason it is only sorting the first number of the code so for example 6 would be on top and 50000 would be on the bottom, how would you fix this? – Daniel Jun 09 '15 at 11:45
  • @Daniel Perhaps you have your scores provided as strings (`"6"`) instead of numbers (`6`). – dlask Jun 09 '15 at 13:26