-1

Need to set up an array (or list) in the following format: 12 100 200, 14 122 134, 1 456 218, 89 23 844 and so on. These are input by the user; using the code I've written `

while(i < number): 
    s = input()
    a, b, c = s.split(' ')
    intermediate_a = int(a)
    intermediate_b = int(b)
    intermediate_c = int(c)
    id.append(intermediate_a)
    game_one.append(intermediate_b)
    game_two.append(intermediate_c)

    i = i + 1

I have stored the ids, game1 scores and game2 scores in different lists. I am trying to store them in one array/list so that I can sort them based on different columns (either game1 score or game2 score). First, I am trying to sort all the players based on their scores (game_one) and rank them (1, 2, 3...) accordingly. I am trying to do that and add up another column with the gamer ranks. I am trying to do the same WITHOUT using numpy, just lists or arrays. Any help would be awesome. Thanks.

quarters
  • 207
  • 1
  • 4
  • 12
  • Duplicate: http://stackoverflow.com/questions/4174941/how-to-sort-a-list-of-lists-by-a-specific-index-of-the-inner-list – Sandman May 28 '15 at 02:42

1 Answers1

0

First of all you can simply use map to get a list from raw_input:

user_scores = [] # stores in this format[[id, game1, game2]]
while i< number:
    user_input = map(int, raw_input().split())
    user_scores.append(user_input)
    i += 1

Secondly to sort them you can use your own defined key functions and pass to sorted method as:

def sort_id(var):
    return var[0]
def sort_game1(var):
    return var[1]
def sort_game2(var):
    return var[2]

sorted(user_scores, key = sort_id) # gets sorted on id
sorted(user_scores, key = sort_game1) # gets sorted on game 1
sorted(user_scores, key = sort_game2) # gets sorted on game2
shaktimaan
  • 1,769
  • 13
  • 14
  • Thanks! But, I seem to be getting an error, `Traceback (most recent call last): File "C:/Python34/all_tests.py", line 27, in print(sorted(user_scores, key = sort_id)) # gets sorted on id File "C:/Python34/all_tests.py", line 17, in sort_id return var[0] TypeError: 'int' object is not subscriptable` when I try to print it. – quarters May 28 '15 at 04:00
  • my bad, forgot to append `user_input` to `user_scores`, just corrected it. Now it should not throw any error – shaktimaan May 28 '15 at 05:01