I have a text file data on certain people for exa: Harry = 10 David = 11 Goliath = 13 in each new line
I want to make it such that when i print it displays: Goliath = 13 David = 11 Harry = 10
I have a text file data on certain people for exa: Harry = 10 David = 11 Goliath = 13 in each new line
I want to make it such that when i print it displays: Goliath = 13 David = 11 Harry = 10
Python's sorted
takes a reverse
keyword. Just set that to True
and you're all set.
e.g.:
>>> print(sorted(range(10), reverse=True))
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Or, in your case:
sorted_data=sorted(file.readlines(), reverse=True, key=lambda item: int(item.rsplit('=',1)[-1].strip()))