In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces how to use Python to obtain the corresponding value of the dictionary key related knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe you will get something after reading this article on how to use Python to obtain the corresponding value of the dictionary key, let's take a look.
When you know the keys of a dictionary: unit_rooms= {3: {301: [1pje 80], 302: [1rect 80], 303: [2je 90]}, 4: {401: [1jue 80], 402: [1je 80], 403: [2je 90], 404: [2je 90]}, 5: {501: [1jue 80], 502: [1jue 80], 503: [2jue 90] For i in range: rooms=unit_ [I] print (rooms)
Running result:
When you don't know the keys of the dictionary: unit_rooms = {3: {301: [1jue 80], 302: [1jue 80], 303: [2jue 90]}, 4: {401: [1jue 80], 402: [1je 80], 403: [2jue 90], 404: [2jue 90]}, 5: {501: [1jue 80], 502: [1jue 80], 503: [2jue 90] For rooms in unit_rooms.values (): print (rooms)
Running result:
Get the value of the dictionary:
For value in DictName.values (): the name of # value can be chosen by yourself. # DictName is the name of the dictionary to be traversed. Values (): is a fixed usage
Get the key value:
For kjournal v in DictName.items (): # the key-value pair of the traversal dictionary, the k corresponding key, the v corresponding value # kPowerv can be named by yourself. DictName is the dictionary name unit_rooms = {3: {301: [1pje 80], 303: [2je 80], 303: [2je 90]}, 4: {401: [1je 80], 402: [1je 80], 403: [2jue 90], 404: [2je 90]}, 5: {501: [1m 80], 502: [1m 80], 503: [2jue 90] For sub_dict in unit_rooms.values (): # traverses the value of the big dictionary
Running result:
Direction 1 represents north and south, and direction 2 represents east and west.
To replace a number:
Unit_rooms = {3: {301: [1jue 80], 302: [2jue 90], 303: [2jue 90]}, 4: {401: [1jue 80], 402: [1jue 80], 403: [2jue 90], 404: [2jue 90]}, 5: {501: [1jue 80], 502: [1m 80], 503: [2jue 90], 504: [2jue 90]} for sub_dict in unit_rooms.values (): for room Info in sub_dict.items (): dire = ['', 'north and south', 'east-west'] # create a list The 0th element is empty, the first element is' north-south', and the second element is' east-west 'print (dire [info [0]])
For example:
Unit_rooms = {3: {301: [1jue 80], 302: [2jue 90], 303: [2jue 90]}, 4: {401: [1jue 80], 402: [1jue 80], 403: [2jue 90], 404: [2jue 90]}, 5: {501: [1jue 80], 502: [1m 80], 503: [2jue 90], 504: [2jue 90]} for sub_dict in unit_rooms.values (): for room Info in sub_dict.items (): dire = ['', 'north and south', 'east-west'] print ('bedroom number:% d towards:% s area:% d'% (room,dire [info [0]], info [1]))
Running result:
Attached: the key corresponding to the maximum value of dic in the dictionary
Step: (1) use the max () function to find the maximum value maxValue; (2) apply the custom function of one
Def getKey (dic, value): if value not in dic.values (): return None result = set () for key in dic: if [key] = = value: result.add (key) return result dic = {'avell2,' bounded key 1, 'caterpillar key 10} maxValue=max (dic.values) result = getKey (dic,maxValue) > {' c' This is the end of the article on "how to use Python to get the values corresponding to dictionary keys" Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use Python to obtain the corresponding value of dictionary keys". If you want to learn more, you are welcome to follow the industry information channel.
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.