简体   繁体   中英

Does Dictionary in Python always arranges keys in ascending order?

I have a dictionary which has frequency (int) as keys and numbers (list) as values. If I iterate through the list as given below will it give the frequencies in ascending order.

for freq in dic:
   print(freq)

Will the frequency be in ascending order?

If you know that the items were inserted into the dict in sorted order, recent versions of Python will maintain that order. If you really need to be sure, it's easy to sort them:

for freq in sorted(dic.items()):
   print(freq)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM