2016-04-18 67 views
0

鑑於此陣元組Python的方式進行排序元組,字典項目,並轉換成數組

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

,或者給本字典

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

如何創建的「H」的數組項目['h2', 'h3', 'h1']它是按浮動項目排序?

+2

貌似http://stackoverflow.com/questions/10695139/sort-a-list-of-tuples-by-2nd-item-integer-value的副本和http://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value。 – alecxe

回答

1

有關列表:

>>> 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']