Get the App
SLTechnology News&Howtos  ›  Development  › 

How to analyze dictionary dict and collection set in python

Shulou Source: shulou.com Published: 2022-06-02 05:15:56 09月19日 Update

Today, I will talk to you about how to analyze the dictionary dict and collection set in python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

1. Dictionaries

Dictionary is a data structure in python.

Its content exists in the form of * key-value (key-value) * *.

Dict has a good query speed, and the median value of dict can be any python object. Assign value to a key, and the following value will overwrite the previous value.

Related operation of dictionary

# the contents of the dictionary are placed in curly braces {}, and the key values are separated by colons Use the comma # method 1myd = {'name':' lmy', 'age':' 19'} l = [('name',' lmy'), ('age',' 19')] myd = dict (l) # method 2myd = dict (name='lmy', age='19') # Dictionary query print (myd.get ('name')) print (myd.get (' age'))''lmy19'''

Modify:

# the contents of the dictionary are placed in curly braces {}, and the key values are separated by colons Use the comma # method 1myd = {'name':' lmy', 'age':' 19'} l = [('name',' lmy'), ('age',' 19')] myd = dict (l) # method 2myd = dict (name='lmy', age='19') myd ['age'] = 20 # modify information here # query print (myd.get (' name')) print (myd.get ('age'))' 'lmy20'''

Empty the collection:

Myd.clear () gets the length of the dictionary # the contents of the dictionary are placed in curly braces {}, the key values are separated by colons, and the key values are separated by a comma # method 1myd = {'name':' lmy', 'age':' 19'} l = [('name',' lmy'), ('age',' 19')] myd = dict (l) # method 2myd = dict (name='lmy', age='19') print (len (myd)) # output: 22. Set

Collections (set) store key as well as dictionaries, but only key

Key is not repeatable, so the values in set are unrepeatable and unordered

Duplicate elements in the myset = {'lumped,' masked,'y'} # collection are automatically filtered out s = set (['lumped,' masked, 'yearly,' y`]) print (s) # output: {'yearly,' lumped,'m'} # add elements using the add () or update () method, if the element already exists Then s.add ('i') print (s) # deletes the element using the remove () function s.remove ('m') print (s) # to get the set length print (len (s)) # clear the collection using the clear () function s.clear ()

Output:

{'masked,' yearly,'l'}

{'ixed,' masked, 'yearly,' l'}

{'ified,' yearly,'l'}

three

After reading the above, do you have any further understanding of how to analyze the dictionary dict and collection set in python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Dictionary content between element colon parenthesis comma query analysis function length storage same good median information for this object data data structure Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MySQL MariaDB Microsoft Shulou Information