In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use the For loop to traverse the Python dictionary". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use the For loop to traverse the Python dictionary.
Preface
Before actual combat, we need to create a dictionary of simulation data.
Dict_1 = {'Name':' Zara', 'Age': 7,' Class': 'First','Address':'Beijing'}
Method 1: iterate using For loop + index
The easiest way to traverse a dictionary in Python is to put it directly into a for loop.
Python automatically treats dict_1 as a dictionary and allows you to iterate over its key key. We can then use the index operator to get each value.
For key in dict_1: print (key, ":", dict_1 [key])
If you want to arrange the key keys alphabetically, you can use the sorted () method, as shown below.
For key in sorted (dict_1): print (key, ":", dict_1 [key])
Method 2: iterate using the .keys () + index
You can get the same result as method 1 by using the method of .keys () to return the Python object that contains the dictionary key. Similarly, it needs to be used in conjunction with the index operator.
For key in dict_1.keys (): print (key,'-->', dict_1 [key])
Method 3: iterate with .items ()
In fact, the most "pythonic" and elegant way to traverse a dictionary is to use the .items () method.
Print (dict_1.items ())
To iterate over the keys and values of the transaction_data dictionary, you only need to "unpack" the two items embedded in the tuple, as shown below:
For k in dict_1.items v in dict_1.items (): print (k, ">", v)
It is important to note that k and v are only standard aliases for keys and values, but you can also choose other naming conventions.
For example, we can replace it with an and b, and we will have the same output.
For an in dict_1.items b in dict_1.items (): print (a, "-", b)
Advanced: traversing nested dictionaries
Sometimes, we come across more complex dictionaries-nested dictionaries.
So what should we do about this situation?
Dict_2 = {"num_1": {'Name':' Zara', 'Age': 7,' Class': 'First','Address':'Beijing'}, "num_2": {' Name': 'BOb',' Age': 32, 'Class':' Six','Address':'Shanghai'}, "num_3": {'Name':' Tom', 'Age': 25,' Class': 'Second' 'Address':'Wuhan'}} dict_2
To unlock the key-value pairs that belong to each nested dictionary, we can do this:
For k, v in dict_2.items (): if type (v) is dict: for nk, nv in v.items (): print (nk, "→", nv)
Use the if statement to determine whether the value value is a dictionary, and if so, use the previously mentioned method 3 and iterate with .items ().
The result of the run is as follows.
If you only want to extract some of these dictionaries, you can add conditions to the if statement.
For k, v in dict_2.items (): if type (v) is dict and k = = 'num_2': for sk, sv in v.items (): print (sk, "- >", sv)
Only the dictionary of num_2 is output, and the result is shown below.
Thank you for reading, the above is the content of "how to use For loop to traverse Python dictionary". After the study of this article, I believe you have a deeper understanding of how to use For loop to traverse Python dictionary, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.