In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to locate and traverse the elements of Dict in Python. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Element positioning (key / value positioning) and traversal of dict
An object like dict, we can get a list of all the keys through keys ().
Dict is an object that we can get a list of all values through values ().
With a list, we can traverse the entire dict object.
But use the following styles more often:
Mydict = {'name':' leixuewei', 'date':' 20211104'} print ("traversal dictionary-start") for k, v in mydict.items (): print ("key [% s] = value [% s]"% (k, v)) # print print directly ("traversal dictionary-end")
These lines of code can be copied and run directly.
The following student committee has prepared a more comprehensive code, showing the acquisition of key/value and traversal operations, etc.
#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 11:34 on 2021-11-3 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Ray academic Committee # @ XueWeiTag: CodingDemo# @ File: dict_demo3.py# @ Project: hello#, for example, the information of the academic Committee as a dictionary info_dict = dict () info_dict ['name'] =' LEI_XUE_WEI' # or Changed to 'levin'info_dict [' blogs'] = '93' # 93 blog posts have been published so far. Info_dict ['gongzhong_hao'] =' [Ray Science Committee]'# Welcome to follow and support info_dict ['meta-data'] = dict () print ("contains specific key?" % s "% ('gongzhong_hao' in info_dict) print (' list of all keys:% s'% info_dict.keys ()) print ('list of all values:% s'% info_dict.values ()) print ('get values corresponding to key:', info_dict ['name']) # get but not set to print in info_dict (' get values corresponding to key DemoKey:', info_dict.get ('DemoKey') ) # print ('get the corresponding value of key:', info_dict ['DemoKey']) # this sentence throws an exception: KeyError:' DemoKey'# if there is no key Set the default value and return print ('get the value corresponding to key name:', info_dict.setdefault ('name',' anonymous user Lei Xuewei')) print ('get the corresponding value for key DemoKey:', info_dict.setdefault ('DemoKey',' anonymous user Lei Xuewei')) print ('get the corresponding value for key DemoKey:', info_dict ['DemoKey']) print ("traversal dictionary-start") for key Value in info_dict.items (): print ("\ tkey [% s] = value [% s]"% (key, value)) print ("traversal Dictionary-end") info_dict.clear () # clear the entire dictionary object
The effect is as follows:
These operations are relatively basic, but they must come at hand, so they must be done.
Replication (copy) of dict
The dictionary can be copied through the copy function, but it is a shallow copy, that is, a copy but an incomplete copy
To put it simply, copy key, but the value of the new dictionary is still a reference to the value of the old dictionary.
By the way, I also mention the fromkeys function. This new dictionary only copies the key list, and all the values in the new dictionary are empty.
Having said that, let's take a look at the dictionary code example:
#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 11:34 on 2021-11-3 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Ray academic Committee # @ XueWeiTag: CodingDemo# @ File: dict_demo4.py# @ Project: copy problem of hello'''dict'# as a dictionary info_dict = dict () info_dict ['name'] = 'LEI_XUE_WEI' # or changed to' levin'info_dict ['blogs'] =' 93' # 93 blog posts have been published so far. Info_dict ['gongzhong_hao'] =' [Ray Committee]'# Welcome to follow support info_dict ['meta-data'] = dict () # Select specified key list partial copy keys_copied_dict = info_dict.fromkeys ([' name', 'blogs',' zone']) print ("keys_copied_dict:" Keys_copied_dict) # shallow copy / / Shadow Copy: full copy but not complete copy copied_dict = info_dict.copy () print ("copied_dict:", copied_dict) shadow_copied_dict = info_dict.copy () print ('shadow_copied_dict:', shadow_copied_dict) # # hint: the following line modifies the value corresponding to the key' meta-data' We did not directly modify shadow_copied_dictinfo_dict ['meta-data'] [' date'] ='04 Nov 2021'print ('info_dict:', info_dict) print (' shadow_copied_dict:', shadow_copied_dict) # here the output shows that the change is also passed to shadow_copied_dictprint ("*" * 16 + "empty info_dict") info_dict.clear () print ('info_dict:') Info_dict) # clear print info_dictprint ('shadow_copied_dict:', shadow_copied_dict) # clear after clearing elements print copied_dict after emptying elements
The effect is as follows:
On how to locate and traverse the elements of Dict in Python is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.