Chapter 3 python3, python3
The python official website describes the set as follows:
Python also includes a data typeSets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and specified Ric difference.
Python also includes the data types used for collections. A set is an unordered container without repeated elements. Basic usage includes membership testing and deduplication. The Set object also supports mathematical operations, such as sum, intersection, difference, and symmetry difference.
Features:
- A set is an unordered, non-repeated data combination.
- After a list is changed to a set, deduplication is automatically performed.
- Test the relationship between the intersection, difference set, and union of the two groups of data.
1. Create
Brackets or the set () function can be used to create a set. Note: You must use set () to create an empty set, instead of using {}. The following method creates an empty dictionary.
Instance:
Basket = {'apple', 'Orange ', 'apple', 'pear', 'Orange ', 'banana '} # Use curly brackets to create a set set2 = set ('abracadaba') # Use set () to create a set set3 = set () # create an empty set print (basket)
The above code outputs:
{'banana', 'orange', 'apple', 'pear'}
After careful comparison, you will find that automatic removal of duplicates is worth mentioning that the display order does not indicate that the Sets are sorted.
2. Add Elements
The add (key) method can be used to add elements to the set. You can add them again, but it does not work:
Basket = {'apple', 'Orange ', 'apple', 'pear', 'Orange ', 'bana'} # Use curly brackets to create a set print ('original basket: ', basket) basket. add ('apple') # add a duplicate print ('Add a duplicate entry after: ', basket) basket. add ('watermelon') # add a print that does not repeat ('add an entry that does not repeat: ', basket)
The above code outputs:
The original basket: {'apple', 'Banana ', 'pear', 'Orange '} after adding a duplicate item: {'apple', 'Banana', 'pear ', 'Orange '} after adding a non-repeated item: {'banana', 'apple', 'watermelon', 'Orange ', 'pear '}
3. Delete Elements
You can use the remove (key) method to delete elements:
Basket = {'apple', 'Orange ', 'apple', 'pear', 'Orange ', 'bana'} # Use curly brackets to create a set print ('original basket: ', basket) basket. remove ('apple') # Delete 'apple' print ('delete \ 'apple \ ':', basket)
The above code outputs:
The original basket: {'apple', 'pear ', 'Banana', 'Orange '} deletes 'apple': {'pear', 'Banana ', 'Orange '}
The only difference between set and dict is that the corresponding value is not stored. However, the principle of set is the same as that of dict. Therefore, it cannot be placed into a variable object, because it is impossible to determine whether two mutable objects are equal, it is impossible to ensure that "there will be no repeated elements" in the set ". Put list in set to see if an error is reported.
Set can be regarded as a set of unordered and non-repeating elements in the mathematical sense. Therefore, the two sets can be used for intersection and Union operations in the mathematical sense. We will use an example to demonstrate this:
A = {'apple', 'Orange ', 'pear', 'bana'} # create A set in curly brackets. B = {'apple', 'Orange ', 'watermelon'} C = {'apple', 'Orange '} D = {'C ++', 'python'} print ('set A: ',) print ('set B: ', B) print ('intersection of A and B:',. intersection (B) # print ('a, B's union: ',. union (B) # union set print ('a, B's difference set: ',. difference (B) # difference set (items in A but not in B) print ('a, B's symmetric difference set: ',. symmetric_difference (B) # symmetric difference set (items in A or B but not both) # subset (judge whether A contains all values in B) print ('B is A subset of? ', B. issubset (A) print ('C is A subset of? ', C. issubset (A) # parent set (whether A is the parent set of B) print ('A is the parent set of B? ', A. issuperset (B) print ('A is the parent set of C? ', A. issuperset (C) # No intersection (whether there is no intersection between the two sets) print (whether there is an intersection between A and D? ', A. isdisjoint (D) print ('Is there an intersection between A and B? ',. Isdisjoint (B) print () # output blank line print ('another writing method ') print ('set A:',) print ('set B: ', B) # print (intersection of 'a and B => A & B:', A & B) # intersection print (Union set of 'a and B => A | B: ', A | B) # Union set print (difference set of 'a and B => A-B: ', A-B) # difference set (items in A but not in B) print ('symmetric difference set of A and B => A ^ B :', A ^ B) # symmetric difference set (items in A or B but not both)
The above code outputs:
Set A: {'apple', 'Orange ', 'pear', 'bana'} Set B: {'apple', 'Orange ', 'watermelon'} intersection of A and B: {'apple', 'Orange '} union of A and B: {'apple', 'watermelon', 'pear ', 'Orange ', 'Banana'} difference set of A and B: {'pear ', 'Banana'} symmetric difference set of A and B: {'watermelon', 'pear ', 'banana '} is B A subset of? Is FalseC A subset of? Is TrueA the parent set of B? Is FalseA the parent set of C? Is there an intersection between TrueA and D? Is there an intersection between TrueA and B? False is another method of writing A: {'apple', 'Orange ', 'pear', 'Banana '} Set B: {'apple', 'Orange ', 'watermelon'} intersection of A and B => A & B: {'apple', 'Orange '} union of A and B => A | B: {'apple ', 'watermelon', 'pear ', 'Orange', 'bana'} The difference set of A and B => A-B: {'pear ', 'bana'} symmetric difference set of A and B => A ^ B: {'watermelon', 'pear', 'bana '}