Other built-in methods of collections

1. Cross complement (after joining together minus the common part = union minus intersection)

r = ["hehe","haha","woaini"]
v = ["hehe","haha","nishishabi"]
p = set(r)
y = set(v)
print(p.symmetric_difference(y))
print(p^y)

{ ' nishishabi ' , ' woaini ' }
{ ' nishishabi ' , ' woaini ' }

2.print(p.difference_update()) is to reassign the difference

3.print(p.isdisjoint(y)) to determine whether there is an intersection, if not, true

r = ["hehe","haha","woaini"]
v = ["hehe","haha","nishishabi"]
p = set(r)
y = set(v)
print(p.isdisjoint(y))

false

4.print(p.issubset(y)) is it a subset, if yes then True

r = ["hehe","haha","woaini"]
v = ["hehe","haha","nishishabi"]
p = set(r)
y = set(v)
print(p.issubset(y))

5.print(p.issuperset(y)) is it a superset

6.p.update(y) update, which is different from the collection, it will be re-assigned, add can only increase one value, this can add multiple values ​​add to update one value, union does not update

r = ["hehe","haha","woaini"]
v = ["hehe","haha","nishishabi"]
p = set(r)
y = set(v)
p.update(y)
print(p)

{'woaini', 'hehe', 'nishishabi', 'haha'}

 

 

 

 

Guess you like

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