“This is the fourth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
Official Python column # 29, stop! Don’t miss this zero-based article!
Dict concepts and additions and deletions are shown in front of the committee! This time we have supplemented the introduction of other functions.
Dict element positioning (key/value positioning) and traversal
Dict is an object where we can get a list of all the keys from keys(); Dict objects we can get a list of all the values from values().
This gives us a list to walk through the dict object.
But use the following style more often:
Mydict = {'name': 'leixuewei', 'date': '20211104'} print(" go to ----- ") for k, v in mydict.items(): Print (" key = value [% s] [% s] "(k, v)) % # print directly print (" traversal dictionary -- -- -- -- -- end")Copy the code
These lines of code can be directly copied to run.
The following committee has prepared more comprehensive code, showing the key/value fetch and traversal operations, etc. :
#! /usr/bin/env python # -*- coding: utf-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: Dict_demo3.py # @Project: For example, the committee's information is taken as a dictionary info_dict = dict() info_dict['name'] = 'LEI_XUE_WEI' # or changed to 'levin' info_dict['blogs'] = '93' # So far, 93 blog posts have been published. Info_dict ['meta-data'] = dict() print(" contains specific key? %s" % ('gongzhong_hao' in info_dict) print(' gongzhong_hao' in info_dict) print(' gongzhong_hao' in info_dict) %s' % info_dict.values()) print(' get the value of the key: ', info_dict['name']) ', info_dict. Get ('DemoKey', info_dict['DemoKey'])) # print(' fetch key: ', info_dict['DemoKey']) # print(' fetch key: ', info_dict['DemoKey']) 'DemoKey' # If there is no key, set the default value and return print(' get the value of key name: ', info_dict.setdefault('name', 'name')) print(' obtain the value of key DemoKey: ', info_dict.setdefault('DemoKey', 'DemoKey')) print(' get the value of key DemoKey: ', info_dict['DemoKey']) print(" go through the dictionary ----- start ") for key, value in info_dict.items(): Print (" \ tkey value [% s] [% s] = "% (key, value)) print (" traversal dictionary -- -- -- -- -- end") an info_dict. The clear () # to empty the whole dictionary objectCopy the code
The effect is as follows:
These operations are relatively basic, but must be readily available, so must be hammered.
Dict copy (copy)
A copy of a dictionary can be done using copy, but it is a shallow copy, that is, a copy but not a full copy
Simply copy the key, but the value of the new dictionary is still referenced by the value of the old dictionary.
By the way, the FromKeys function creates a new dictionary that just copies the list of keys. All values in the new dictionary are empty.
With that said, let’s look at an example of dictionary code:
#! /usr/bin/env python # -*- coding: utf-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: Dict_demo4.py # @Project: For example, info_dict = dict() info_dict['name'] = 'LEI_XUE_WEI' # or instead 'levin' Info_dict ['blogs'] = '93' # Currently posts 93 blogs. Info_dict ['meta-data'] = dict() # select a local copy of keys_copied_dict = Info_dict. Fromkeys (['name', 'blogs', 'zone']) print("keys_copied_dict :", keys_copied_dict) Copied_dict = info_dict. Copy () print("copied_dict :" Copied_dict) shadow_copied_dict = info_dict.copy() print('shadow_copied_dict:', shadow_copied_dict) ## The next line of code modifies the value of the 'meta-data' key, Shadow_copied_dict info_dict['meta-data']['date'] = '04 Nov 2021' print('info_dict:', info_dict) print('shadow_copied_dict:', Shadow_copied_dict print("*"*16 + "clear info_dict") info_dict.clear() Print ('info_dict:', info_dict:', info_dict:') Shadow_copied_dict) # clear Prints copied_dict after the element is clearedCopy the code
The effect is as follows:
conclusion
Dict is pretty smooth to use in general, except for copying, which will be covered in the next academy post.
Dict is expected to be mastered by beginners.
By the way, if you like Python, please check out the Committee’s Python Basics column or Python Getting Started to Master column
Continuous learning and continuous development, I am Lei Xuewei! Programming is fun. The key is to get the technology right. Welcome to wechat, like support collection!