-1

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

pman
  • 33
  • 9

1 Answers1

1

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()))
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • @tzaman -- Yep. Sorry about that. `reversed` is the builtin -- which would also work to get a reversed iterator over the data (but doesn't work for just printing). – mgilson Sep 17 '15 at 21:30
  • Yes i have heard of this but idk where to add reverse=True – pman Sep 17 '15 at 21:31