Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the common operations of Python collection

Shulou Source: shulou.com Published: 2022-06-03 04:56:13 09月15日 Update

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.

Tags: Elements common use intersection examples content subsets that is subsets skills methods knowledge supersets concise concise two functions dictionaries characters strings meaning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Information Shulou Technology MariaDB NVidia