2
f1 = open("leader")
lines = f1.readlines()
lines.sort(key=int, reverse=True)
f1.close()
print(lines)

with external file values:

345345:player7
435:zo
345:name
23:hello
1231:football

this is to sort them so that the integers are sorted not the names

nabster
  • 1,561
  • 2
  • 20
  • 32
leo
  • 31
  • 5
  • i also need help on adding other values without deleting everything in the external file – leo Jan 02 '19 at 19:16
  • You will need to open the file using `'a'` which stands for append. `f1 = open("leader", 'a')` – hqkhan Jan 02 '19 at 19:22
  • It is preferred if you can post separate questions instead of combining your questions into one. That way, it helps the people answering your question and also others hunting for at least one of your questions. Please see [ask] and [The perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). It is unclear if you have problems putting data into datastrucures, reading data from file, sorting data, inserting data (dicts are unordered or insertordersorted- not by numbers) or if you can not save data. – Patrick Artner Jan 02 '19 at 19:24
  • What you most probably want is this: [How to create a highscore in python](https://stackoverflow.com/questions/16726354/saving-the-highscore-for-a-python-game) – Patrick Artner Jan 02 '19 at 19:26

3 Answers3

4

IIUC:

l = ['345345:player7',
'435:zo',
'345:name',
'23:hello',
'1231:football']

sorted(l, key=lambda x: int(x.split(':')[0]))

Output:

['23:hello', '345:name', '435:zo', '1231:football', '345345:player7']
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • 1
    Don't you need to parse the digits into `int` when sorting since OP mentioned `sorting the numbers`? – hqkhan Jan 02 '19 at 19:23
  • 2
    well, it _appears_ to work, but doesn't perform a numerical sort, just a lexicographical sort. 1231 should appear after 435 for instance. – Jean-François Fabre Jan 02 '19 at 19:23
  • 1
    Right, it'll definitely not throw an error but I think OP wanted @Jean-FrançoisFabre's answer here. – hqkhan Jan 02 '19 at 19:24
  • however this does not work as it has to be outputted from an external file and outputs this: – leo Jan 03 '19 at 11:41
4

The sort key should do: "split once, convert to integer". Not converting to integer fails because then lexicographical compare is used and in that case "10" < "2", not that you want.

l = ['345345:player7',
'435:zo',
'345:name',
'23:hello',
'1231:football']

result = sorted(l, key=lambda x: int(x.split(':',1)[0]))

result:

['23:hello', '345:name', '435:zo', '1231:football', '345345:player7']

that doesn't handle the tiebreaker where numbers are equal. A slightly more complex sort key would be required (but still doable). In that case, drop lambda and create a real function so you can perform split once & unpack to convert only the first part to integer:

def sortfunc(x):
    number,rest = x.split(':',1)
    return int(number),rest

result = sorted(l, key=sortfunc)
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
0

Try this: (helpful if you are still reading from a file)

with open('leader.txt', mode = 'r') as f1:
    data = f1.readlines()
# end with
keys = {}
output = []
for s in data:
    num, value = s.split(sep=':')
    if keys.get(int(num), False):
        keys[int(num)].append(value)
    else:
        keys[int(num)] = [value]
for num in sorted(keys):
    for i in keys[num]:
        output.append((num, i))
for num, value in output:
    print(f'{num}: {value}')
GeeTransit
  • 1,458
  • 9
  • 22