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 analyze dictionary dict and collection set in python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

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.

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