*Memo:
- My post explains set and frozenset functions (1).
- My post explains set and frozenset functions (2).
- My post explains set and frozenset functions (3).
- My post explains set and frozenset functions (5).
- My post explains set and frozenset functions (6).
- My post explains set and frozenset functions (7).
- My post explains a set (1).
- My post explains a frozenset (1).
difference() can return the elements in the set or frozenset which aren't in *others (Difference: A - B) as shown below:
*Memo:
- The 1st arguments are
*others(Optional-Default:()-Type:Iterable):- Don't use any keywords like
*others=,others=, etc.
- Don't use any keywords like
-
difference()creates a copy. -
-can dodifference(), creating a copy and supportingsetandfrozenset.
<Set>:
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {30, 40}
print(A.difference(B))
print(A - B)
# {40, 20}
print(A.difference(C))
print(A - C)
# {10, 20}
print(B.difference(C))
print(B - C)
# {10, 50}
print(A.difference(B, C))
print(A - B - C)
# {20}
print(A.difference())
# {40, 10, 20, 30}
<Frozenset>:
A = frozenset([10, 20, 30, 40])
B = frozenset([10, 30, 50])
C = frozenset([30, 40])
print(A.difference(B))
print(A - B)
# frozenset({40, 20})
print(A.difference(C))
print(A - C)
# frozenset({10, 20})
print(B.difference(C))
print(B - C)
# frozenset({10, 50})
print(A.difference(B, C))
print(A - B - C)
# frozenset({20})
print(A.difference())
# frozenset({40, 10, 20, 30})
difference_update() can return the elements in the set which aren't in *others (Difference: A - B) as shown below:
*Memo:
- The 1st arguments are
*others(Optional-Default:()-Type:Iterable):- Don't use any keywords like
*others=,others=, etc.
- Don't use any keywords like
-
difference_update()doesn't create a copy. -
difference_update()doesn't exist for a frozenset. -
-=with or without|can dodifference_update(), creating a copy and supportingsetandfrozenset.
<Set>:
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {30, 40}
A_ = A.copy()
A_.difference_update(B)
A_ -= B
print(A_)
# {20, 40}
A_ = A.copy()
A_.difference_update(C)
A_ -= C
print(A_)
# {20, 10}
B_ = B.copy()
B_.difference_update(C)
B_ -= C
print(B_)
# {10, 50}
A_ = A.copy()
A_.difference_update(B, C)
A_ -= B | C
print(A_)
# {20}
A_ = A.copy()
A_.difference_update()
print(A_)
# {40, 10, 20, 30}
<Frozenset>:
A = frozenset([10, 20, 30, 40])
B = frozenset([10, 30, 50])
C = frozenset([30, 40])
A_ = A.copy()
A_ -= B
print(A_)
# frozenset({40, 20})
A_ = A.copy()
A_ -= C
print(A_)
# frozenset({10, 20})
B_ = B.copy()
B_ -= C
print(B_)
# frozenset({10, 50})
A_ = A.copy()
A_ -= B | C
print(A_)
# frozenset({20})
Top comments (0)