In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you what are the common operations of the Python collection, which are concise and easy to understand, which can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
First, what is a collection?
A set is similar to a dictionary (dict) in that it is a collection of sets of key, but does not store value. The property of the collection is that key cannot be repeated.
Second, common operations of collection
1. Create a collection
Set can be created using {} or the set function:
S1 = {'averse,' baked, 'cased,' axed, 'dashed,' b'} # use {} print (S1) (['axiao,' centering, 'baking,' d']) S2 = set ('helloworld') # use set () Receive a string print (S2) set (['enumbers,' dwells, 'hags,' lags, 'oaths,' renders,'w']) S3 = set (['.mp3', '.mp4', '.rmvb', '.mkv', '.mp3']) # using set (), receive a list print (S3) set (['.mp3', '.mkv', '.rmvb', '.mp4'])
two。 Ergodic set
S = {'averse,' baked, 'caged,' axed, 'dumped,' b'} for e in s: print (e)
3. Add element
The add () method can add elements to the set, which can be added repeatedly, but with no effect.
S = {'averse,' baked, 'caged,' axed, 'dumped,' b'} print (s) set (['averse,' cased, 'baked,' d']) s.add ('e') print (s) set (['averse,' cased, 'baked,' eBay,'d']) s.add ('a') print (s) set (['aqu,' cased,'b']) 'eBay,' d']) s.add (4) print (s) set (['asides,' canals, 'baked, 4,' dudes,'e'])
4. Delete element
The remove () method removes elements from the collection, but deleting elements that don't exist throws a KeyError instead of discard ().
Example:
S = {'averse,' baked, 'caged,' averse, 'dumped,' b'} print (s) set (['a') # delete element'a' print (s) set (['cased,' baked,'d']) s.remove ('e') # Delete elements that do not exist, KeyError will be thrown
S = {'averse,' baked, 'caged,' averse, 'dumped,' b'} print (s) set (['a') # delete element'a' print (s) set (['cased,' baked,'d']) s.discard ('e') # Delete elements that do not exist and will not throw KeyError
Third, intersection / union / difference
The set in Python can also be regarded as a set of disordered and non-repetitive elements in the mathematical sense, so we can cooperate with the intersection and union of the two sets.
Look at the example:
S1 = {1,2,3,4,5,6} S2 = {3,6,9,10,12} s3 = {2,3,4} print (S1 & S2) # intersection set ([3,6]) print (S1 | S2) # Union set ([1,2,3,4,5,6,9,10,12]) print (S1-S2) # difference set set ([1,2,4]) 5]) print (s3.issubset (S1)) # S3 is a subset of S1 print (s3.issubset (S2)) # S3 is a subset of S2 print (s1.issuperset (S2)) # S1 is a superset of S3 print (s1.issuperset (S2)) # S1 is a superset of S2
The above are the common operations of the Python collection. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.