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

How to realize Dictionaries Operation in python

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about how to achieve Dictionaries operation in python. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

First, create a new python file named py3_dict.py, and write the string manipulation code in this file (the following is the code, which shows the running effect):

# dictionaries is a collection of Key-Value pairs # define a dictionary student = {'name':'yale','age':25,'course': [' mathematics', 'computer']} print (student) print (student ['name']) print (student [' course']) # the key and value of the dictionary can be defined as immutable data type#, for example: define key as 1student = {1name':'yale','age':25,'course': ['mathematics']. 'computer']} print (student [1]) # access to a non-existent key# will cause an exception # KeyError: 'phone'student = {' name':'yale','age':25,'course': ['Mathematics' 'computer']} # print (student ['phone']) # sometimes we want key# that does not exist to return None or a default value # implemented as follows: print (student.get (' phone')) # Noneprint (student.get ('phone',' not found')) # return default value: not found # add data to dict dictionary student = {'name':'yale','age':25,'course': [' Mathematics') 'computer'} student ['phone'] =' 010-55555555'print (student.get ('phone',' not found')) # 010-55555555 change the value corresponding to the existing key student = {'name':'yale','age':25,'course': [' mathematics', 'computer']} student ['name'] =' andy'print (student) # use update () to change multiple values in the dictionary student = {'name':'yale' 'age':25,'course': [' Mathematics', 'computer']} student.update ({'name':'andy','age':26 'phone':'12345678'}) print (student) # delete a key# using the del keyword del student [' phone'] print (student) # or using the previously mentioned pop () method # delete data age = student.pop ('age') print (age) # 26print (student) # use len () to see how many keystudent = {' name':'yale','age':25,'course': ['mathematics') are in the dictionary Computer]} print (len (student)) # "View all keyprint (student.keys ()) # dict_keys (['name',' age', 'course']) # View all valueprint (student.values ()) # dict_values ([' yale', 25, ['Mathematics', 'computer']]) # View all key and value# to get a pair of key-value#dict_items ([(name', 'yale')) ('age', 25), (' course', ['Mathematics', 'computer'])) print (student.items ()) # Loop Dictionary # Loop like list What is printed is the key value # name#age#coursefor key in student: print (key) # so we use the items () method to cycle through the data: for key,value in student.items (): print (key,value) # result: # name yale#age 25#course ['math', 'computer']

The running effect of the above code:

{'name':' yale', 'age': 25,' course': ['mathematics', 'computer']} yale ['mathematics', 'computer'] yaleNone could not find 010-55555555 {'name':' andy', 'age': 25,' course': ['mathematics', 'computer']} {'name':' andy', 'age': 26,' course': ['mathematics', 'computer'] 'phone':' 12345678} {'name':' andy', 'age': 26,' course': ['mathematics', 'computer']} 26 {'name':' andy', 'course': [' mathematics', 'computer']} 3dict_keys (['name',' age', 'course']) dict_values ([' yale', 25, ['mathematics') 'computer']) dict_items ([('name',' yale'), ('age', 25), (' course', ['Mathematics', 'computer')]) nameagecoursename yaleage 25course ['Mathematics', 'computer'] this is how to implement the Dictionaries operation in the python shared by Xiaobian. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are 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

Internet Technology

Wechat

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

12
Report