Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of deletion and copy of python Dictionary

2025-05-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you the python dictionary deletion and copy of the example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

Delete and copy clear function of dictionary

The function of the clear function: to clear the existing data in the current dictionary

Usage of clear function: dict.clear (), no parameters, no return value

Examples are as follows:

User = {'name':'Neo',' age':18, 'birthday':'2000-01-01'} user.clear () print (user) # the execution result is as follows: # > {} pop function

The function of the pop function: delete the key specified in the dictionary, return its result, and report an error if the key does not exist.

The usage of the pop function: dict.pop (key), delete the key specified in parentheses and return the value corresponding to this key.

Examples are as follows:

User = {'name':'Neo',' age':18, 'birthday':'2000-01-01'} pop_value = user.pop ('birthday') print ('\ 'pop_value\' deleted\ 'value\' of\ 'birthday\' is:', pop_value,'\ 'user\' dictionary is:' User) # the execution result is as follows: # > 'pop_value'' value' is: 2000-01-01 'user' dictionary is: {' name': 'Neo',' age': 18} del function

Function of the del function: delete the key specified in the dictionary or delete the entire dictionary

Usage of del function: del dict ['key'], del dict

Examples are as follows:

User = {'name':'Neo',' age':18, 'birthday':'2000-01-01'} del user ['birthday'] print ('\ 'user\' dictionary is:', user) # the execution result is as follows: # > 'user' dictionary is: {' name':'Neo', 'age':18} del userprint (user) # execution result is as follows: # > NameError: name' user' is not defined. Did you mean: 'super'?# the user variable here has been completely deleted from the copy function

The function of the copy function: copy the current dictionary to a new dictionary, which does not share the same memory address as the original dictionary.

Usage of the copy function: dict.copy (), which takes no arguments and returns a dictionary with exactly the same content but a different memory address.

Examples are as follows:

Old_user = {'name':' Neo', 'age': 18,' birthday': '2000-01-01'} new_user = old_user.copy () print (old_user) print (new_user) # the execution result is as follows: # > > {'name':' Neo', 'age': 18,' birthday': '2000-01-01'} # > > {'name':' Neo', 'age': 18 'birthday':' 2000-01-01'} print (the memory address of'"old_user" is:', id (old_user),'"new_user" is:', id (new_user)) # the execution result is as follows: the memory address of "old_user" is: 140464840867968 "new_user": 140464841281088 is all the contents of the article "sample Analysis of deletion and copy of python Dictionary" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report