Collection of python

a={1,2,3,4,"aaa"}

1. Add

a.add()

例:a.add(5)

a.update

例:a.update([1,2,3,"bbb"])

Difference: add can only add one number, update can add multiple

 

2. Delete

a.pop()

Example: a.pop ()

a.remove()

例:a.remove(2)

a.discard()

例:a.discard()

Difference: pop is to delete one randomly, remove is to specify to delete one (but an error will be reported if there is no such number in the set), and discard is also a specified number of deletions (but no error will be reported if there is no such number in the set)

a.clear() clear

例:a.clear

 

3. Intersection, difference, complement

Intersection
a.intersection(b)
a&b

Difference
(a.difference(b))
(ab)

union
(a.union(b))
a|b

4. Symmetrical intersection, calculate the part of the two unions to remove the intersection

print(a.symmetric_difference(b))

 

5. Subset, Superset

print(a.issubset(b))#Judging that a is a subset of b

print(a.issuperset(b))#To judge that a is a superset of b,
you can also use >= or <=

a.isdisjoint(b)#Is it disjoint

a.difference_update(b)#Assign the difference of a and b to a
a.intersection_update(b)#Assign the combination of a and b to a

 

Guess you like

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