python collection set learning summary

1. Representation of collection set : enclosed in curly brackets

Such as

s = {1,2,33,33,33,44,55,66,7,8,9,'abc'}	
a. The collection set is composed of different elements, that is to say, the collection set does not contain duplicate elements . When printing(s), you will find that there is only one left of 33 (# That is, if there is a string For things like lists, if you intend to remove duplicate elements but do not need to consider the order, you can directly convert them to sets and then convert them back )
b. The elements in the set are unordered , which is similar to the dictionary
c. Sets The elements in the set must be of immutable type, that is to say, the set can only have integer int, string str and tuple tuple

DDDDD. You can define an immutable collection, defined with frozenset . for example

s = frozenset('hello')	

2. Add, delete, modify and check the collection set

a. add

.add() (element to be added) [Add the specified element to the collection set, and no error will be reported if the element is repeated]

b. delete

.clear() [Clear all elements in the specified set]
.pop() [Clear a random element in the set]
.remove() (specified element) [Clear the specified element in the list, if it cannot be found Error]
.discard() (specified element) [clears the specified element in the list, compared to remove, discard cannot be found and no error will be reported]

c. Modify

.update() (the specified iterable object) [This method is equivalent to .union_update, that is, the union of the two functions is obtained and updated to the original function]

d. Inquiry

It can be output by means of a for loop

3. The operation of the set set

a. Intersection

.intersection() (specified set) [returns the intersection of the current set and the specified set]
The & operator can also be used to represent the intersection of the two sets
.intersection_update() (specified set) [On the basis of intersection(), Update the result to the original set]

b. Union

.union() (specified set) [returns the union of the current set and the specified set]
Similarly, the | operator can be used to represent the union of two sets

c. Difference

.difference() (subtracted set) [return the difference set produced by subtracting another set from the subtracted set]
Use the - operator to represent the difference set of the two sets, where the difference set means the 'subtracted set' Elements that exist in the set, but do not exist in the subtraction set. For
example : {5,4,3,2,1} - {9,8,7,6,5,4} = {3,2,1}
.difference_update( ) (subtracted set) [On the basis of difference(), update the result to the original set (subtracted set)]

d. Cross complement

.symmetric_difference() (specified set) [Output the cross complement of two sets]
What is the cross complement, that is, the two sets are combined and then the elements common to the two sets are subtracted, and the rest is the cross complement of the two sets Sets can
also use the ^ operator to achieve the effect of cross complement.symmetric_difference_update
()


4. Other methods

.isdisjoint() (the set to be judged with the original set) [return true when the intersection of the two sets is an empty set]
.issubset() (the set to be judged with the original set) [when the original set is a child of the target set When set, return true]
.issuperset() (the set to be judged with the original set) [contrary to the above, when the original set is the parent set of the target set, return true]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325827865&siteId=291194637