In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to understand the addition, deletion and modification of dictionaries in Python? in view of this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
Dictionaries in Python
I. the characteristics of dictionaries
Second, create a dictionary
Create a dictionary with curly braces
Dict1= (dict1) dict2=dict (dict2 ['a']) # # first creation method print (dict2) {'axiajug 3) # # first creation method print (dict2) {' aquired: 2, 'baked: 3} 3, query dictionary data dict2=dict (dict2 [' a']) # # create dictionary print (dict2 ['a']) # # query the value of an in the dictionary If key does not exist, throw an exception print (dict2.get ('a')) # # so that the get method queries the value of an in the dictionary. If key does not exist, it returns none224, the judgment dict2=dict of Key (axi2job3) print ('a`in dict2) print (' a`not in dict2) TrueFalse 5, and the deletion dict2=dict of Key (axi2job3) # # delete Clear for cleardel dict2 ['a'] print (dict2) {'baked: 3} VI. Add dict2=dict for Key (aq2jobbing 3) # # dict2 [' c'] = 4print (dict2) {'afid: 2,' baked: 3, 'cased: 4} VII, modification of Key dict2=dict (aq2jobbing 3) # # the first way to create dict2 [' c'] = 4 # add c The value is 4print (dict2) dict2 ['c'] = 10 # # the value of modified c is 10print (dict2) {'averse: 2,' baked: 3, 'cantilever: 4} {' axiang: 2, 'baked: 3,' cased: 10} # c has been modified to 10 / 8, dictionary view keys ()
Get all the key in the dictionary
Test= {'Zhang San': 56 Magneto'Li Si': 56} print (test.keys ()) print (list (test.keys ()) # # convert to list dict_keys (['Zhang San','Li Si']) ['Zhang San','Li Si'] values ()
Get all the values in the dictionary
Values=test.values () print (values) dict_values ([56, 56]) [56, 56] iterms ()
Get all the key,values pairs in the dictionary
Iterm=test.items () print (iterm) print (list (iterm)) # # converted list element is a group of meta-ancestors Dict _ items ([('Zhang San', 56), ('Li Si', 56)]) [('Zhang San', 56), ('Li Si', 56)] IX. Ergodic dic1= {'a'axiang'. For i in dic1: print (I) # # key print (dic1.get (I)) # # use the get function to print the value print (Dic1 [I]) # # the second method traverses the value ten and generates the dictionary expression name= ['Zhang San','Li Si', 'Wang Wu'] age= [23 name:age for name,age in zip 45] d = {name:age for name,age in zip (name) Age)} # Note to enlarge parentheses print (d) ```d = {test:age for test,age in zip (name,age)} # # has nothing to do with the variable name The value of zip package is related to print (d) # # the result is the same ``{'Zhang San': 23,'Li Si': 45, 'Wang Wu': 37} with brackets print (d) ```d = {test:age for test,age in zip (name,age)} # # has nothing to do with the variable name The value of zip package is the same as print (d) # # the result is the same ``{'Zhang San': 23,'Li Si': 45, 'Wang Wu': 37} this is the answer to the question on how to understand the addition, deletion and revision of dictionaries in Python. I hope the above content can be of some help to all of you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more relevant knowledge.
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.