The order of keys in dictionaries

前端 未结 6 965
渐次进展
渐次进展 2020-11-22 14:32

Code:

d = {\'a\': 0, \'b\': 1, \'c\': 2}
l = d.keys()

print l

This prints [\'a\', \'c\', \'b\']. I\'m unsure of how the metho

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 14:58

    >>> print sorted(d.keys())
    ['a', 'b', 'c']
    

    Use the sorted function, which sorts the iterable passed in.

    The .keys() method returns the keys in an arbitrary order.

提交回复
热议问题