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)06/01 Report--
This article mainly explains "the method of traversing data in python dictionary". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "the method of traversing data in python Dictionary".
Description
1. For loop traversal: use the for loop to directly traverse the dictionary and get the key value of the dictionary.
2. Keys (): used to get the key value of the dictionary. The type obtained is dict_keys, and then cast using list () to get the key value, or iterate through it using for.
3. Values (): used to get the value value of the dictionary of type dict_values, and then use = = list () = = cast to get the value value, or you can use the for loop to traverse.
4. Items (): used to get all the key-value pairs in the dictionary. The obtained type is dict_items, and the content is a tuple type consisting of key and value values.
Example # define a dictionary dic = {"Name": "Zhang San", "Gender": "male", "Age": 20, "Height": 177} # for cycle through dictionary contents for i in dic: print (I, ":" Dick [I]) print ("= =" * 26) # dic.keys traverses print (type (dic.keys ()) # print dic.keys ()) to get the data type for i in dic.keys (): print (I, ":" Dic.values [I]) print ("= =" * 26) # dic.values () traverses print (type (dic.values ()) for i in dic.values (): print (I) print ("= =" * 26) # dic.items () traverses print (dic.items ()) for i in dic.items (): # traverses using a secondary loop The first time to get the contents of the tuple, the second time to get the specific value for j in i: print (j, end= ":") print ()
Instance expansion:
Traverse every key in the dictionary
My_dict = {"name": "Wang Wu", "age": 20} # iterate through every keyfor key in my_dict.keys (): print (key) # output > > name > > age in the dictionary
Traverse every value in the dictionary
My_dict = {"name": "Wang Wu", "age": 20} # iterate through every for value in my_dict.values (): print (value) # output > > Wang Wu > > 20 in the dictionary
Traverse every item of data in the dictionary, each item of data is a key-value pair, encapsulating the key-value pair into the meta-ancestor
My_dict = {"name": "Wang Wu", "age": 20} # traverses every item of data in the dictionary, each data is a key-value pair, encapsulating the key-value pair in meta-ancestor for item in my_dict.items (): print (item) # output > > ("name", "Wang Wu") > > ("age", 20) so far, I believe you have a better understanding of "the method of traversing data in python dictionary" You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.