Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Items method and key pop method popite in 36 dictionaries

Shulou Source: shulou.com Published: 2022-06-03 08:17:28 10月02日 Update

The other side will change the same way.'' The key method is used to return all the key in the dictionary similar to the items method Value)) "" help = help bike = bicycle plane = plane China = China "" print (('bike',' bike') in d.items ()) # True dict_items = d.items () d ['bike'] =' bicycle Motorcycle; electric bicycle 'print (dict_items) # dict_items ([(' help', 'help'), ('bike',' bicycle; motorcycle) Electric bicycle'), ('plane',' plane'), ('China',' China')]) print (d.keys ()) # dict_keys (['help',' bike', 'plane'') 'China']) for key in d.keys (): print (key)' 'helpbikeplaneChina'''--- lesson 11 values in pop-up dictionaries (pop method and popitem method) # pop method and popitem method # pop method Used to get the value of the specified key and pop up the key-value pair # popitem method to return the last key-value pair in the dictionary And pop up the key-value pair d = {'caterpillar 10 precepts for the paraphernalia 40, the paraphernalia 12 for the paraphernalia, 45} d [' 1'] = 20 # first add two groups of elements d ['5'] = 100print (d) # {{'czuv] 10 for the paramedics, 45,' 1: 20 Print (d.pop ('a')) # 40 gets the value of the specified key print (d) # {'cages: 10,' breadth: 12, 'xtrees: 45,' 1levels: 20, '5cycles: 100} a the kv element pops up together # 4values {' cages: 10, 'banners: 12,' xtrees: 45, '1bands: 20 Print (d.popitem ()) # (510,100) for i in range (len (d)): print (d.popitem ())''the output is: (5000,100) (' 1x, 20) ('x, 45) ('baked, 12) (' c' 10) lesson 12 add but not modify value (setdefault method) # setdefault method: if this key does not exist in the dictionary Then add a set of values to the dictionary and return value If the key already exists in the dictionary, the latter value is ignored and the value that exists in the dictionary is returned. If we do not specify the second parameter value, we will return None d = {"name": "Bill", "age": 30} d ['salary'] = 2000d [' age'] = 50 # how this element does not exist in the dictionary, then a set will be added later If there is such a long time to modify his valueprint (d) # {'name':' Bill', 'age': 50,' salary': 2000} # Noneprint (d.setdefault ("location", "China")) print (d)''China {' name': 'Bill',' age': 50, 'salary': 2000,' location': 'China'} 'print (d.setdefault ("location") "Germany") print (d)''China {' name': 'Bill',' age': 50, 'salary': 2000,' location': 'China'} 'print (d.setdefault ("abc")) print (d)' 'None {' name': 'Bill',' age': 50, 'salary': 2000,' location': 'China' 'abc': None}' China {'name':' Bill', 'age': 50,' salary': 2000, 'location':' China'{'name':' Bill', 'age': 50,' salary': 2000, 'location':' China'} None {'name':' Bill', 'age': 50,' salary': 2000, 'location':' China' Lesson 13 importing dictionary data in batches (update method) # update method # updating one dictionary with key-value pairs in one dictionary This method receives a parameter # dictionary used as the data source # dict1.update (dict2) is not inserted, if any Update D1 = {'title':' Ori College', 'website':' https://geekori.com',' description':' engaged in online IT course development and sales'} D2 = {'title':' Ori Technology', 'products': [' Ori College', 'blog', 'Reading Channel', 'geek question bank', 'OriUnity'],' description':' engaged in online IT course development and sales' Tool software development'} d1.update (D2) # using the dictionary of D2 to change D1 does not directly increase, if any, use the dictionary value of D2 to update updateprint (D1) {'title':' Ouri Technology', 'website':' https://geekori.com', 'description':' to develop and sell online IT courses Tools and software development', 'products': [' Ori College', 'blog', 'Reading Channel', 'geek question bank' Lesson 14: get a list of values in a dictionary (values method) # values method: get a list of values in a dictionary Value is repeatable, and there can be several of the same # keys methods: the key that returns the list of key in the dictionary is unique and cannot be repeated. # items method: get all the key and value in the dictionary To combine the for cycle d = {'aprouza 1,' cantilevered 2, 'cantilevered 3,' estranged 4, 'estranged 40} print (d) # {' 'adepravel2,' cantilevered 2, 'dvv3 The value of e in this is overwritten by the following print (d.values ()) # dict_values ([1, 2, 2, 3, 40]) print (d.keys ()) # dict_keys (['averse,' baked, 'cased,' d') 'e']) for value in d.values (): print (value)' '122340''# keys (get key only), values (get value only), items (get key and value)

Tags: Dictionaries methods China bicycles airplanes help elements colleges courses online loops updates sales party median blogs parameters tools tools software motorcycles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Tech Info Apple MySQL Shulou Information