0

Given this array of tuple

[('h1', 0.522611856461), ('h2', 0.438368797302), ('h3', 0.443703174591)]

Or given this dictionary

{'h2': 0.438368797302, 'h3': 0.443703174591, 'h1': 0.522611856461}

How can I create an array of the 'h' items ['h2', 'h3', 'h1'] which is sorted by the float item?

Brad Sturtevant
  • 311
  • 3
  • 13
  • 2
    Looks like a duplicate of http://stackoverflow.com/questions/10695139/sort-a-list-of-tuples-by-2nd-item-integer-value and http://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value. – alecxe Apr 18 '16 at 00:16

1 Answers1

1

For a list:

>>> from operator import itemgetter
>>> l = [('h1', 0.522611856461), ('h2', 0.438368797302), ('h3', 0.443703174591)]
>>> [x[0] for x in sorted(l, key=itemgetter(1))]
['h2', 'h3', 'h1']
pkacprzak
  • 5,537
  • 1
  • 17
  • 37