In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to keep dictionaries orderly in Python". In daily operation, I believe many people have doubts about how to keep dictionaries orderly in Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "how to keep dictionaries orderly in Python"! Next, please follow the editor to study!
1. How to keep the dictionary in order
Actual case:
A programming competition system can time the contestants to solve programming problems. After the contestants finish the problems, record the time taken by the contestants to solve the problems in the dictionary so that the results can be queried according to the contestants' names after the competition. The shorter the time taken to answer the questions, the better the grades.)
For example, the second place in LiLei takes 43 minutes, the fifth place in HanMeimei takes 52 minutes, and the first place in Jim takes 39 minutes
{'LiLei': (2,43),' HanMeimei': (5,52), 'Jim': (1,39),...}
After the competition, we need to print the players' scores according to the ranking order. how to achieve it?
Note: why use a dictionary instead of a list? It is just to make it convenient to check the results by name after the game.
Before version 3.6 of Python, dictionaries are out of order, and after version 3.6, dictionaries begin to order.
Solution:
Using collections.OrderedDict (ordered Dictionary)
Replace the built-in dictionary Dict with OrderedDict, and store the players' scores in OrderDict in turn.
2. Code demonstration (1) OrderedDict ordered dictionary simply use d = dict () print (d) d ['Jim'] = (1,35) d [' Leo'] = (2,37) d ['Bob'] = (3,40) print (d) # when you want to iterate the dictionary Print for k in d in the order of each entry: print (k) # import the ordered dictionary from collections import OrderedDict D2 = OrderedDict () d ['Jim'] = (1,35) d [' Leo'] = (2,37) d ['Bob'] = (3 40) for k in d: print (k) (2) simulating the writing of contest questions # programming simulator players = list ('ABCDEF') # Simulator's answer time from time import timefrom random import randintfrom collections import OrderedDict d = OrderedDict () start = time () for i in range (6): # waiting for user input As a blocking process input () # randomly selected one player at a time to answer the questions p = players.pop (randint (0,5-I)) end = time () print (I + 1, p, end-start) # record the score d [p] = (I + 1, end-start) print () print ('-'* 20) # enter the dictionary order in order Print transcript for k in d: print (k, d [k]) this ends the study of "how to keep dictionaries in order in Python", hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.