*Memo:
- My post explains set and frozenset functions (1).
- My post explains set and frozenset functions (2).
- My post explains set and frozenset functions (4).
- 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).
intersection() can return the zero or more common elements of the set or frozenset and *others
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
-
intersection()
creates a copy. -
&
doesintersection()
, creating a copy and supportingset
andfrozenset
.
<Set>:
A = {10, 20, 30, 40} # set
B = frozenset([10, 30, 50]) # frozenset
C = [30, 40] # list
print(A.intersection(B)) # {10, 30}
print(A.intersection(C)) # {40, 30}
print(A.intersection(B, C)) # {30}
print(A.intersection()) # {40, 10, 20, 30}
A = {10, 20, 30, 40} # set
B = frozenset([10, 30, 50]) # frozenset
C = frozenset([30, 40]) # frozenset
print(A & B) # {10, 30}
print(A & C) # {40, 30}
print(A & B & C) # {30}
print(A) # {40, 10, 20, 30}
<Frozenset>:
A = frozenset([10, 20, 30, 40]) # frozenset
B = {10, 30, 50} # set
C = [30, 40] # list
print(A.intersection(B)) # frozenset({10, 30})
print(A.intersection(C)) # frozenset({40, 30})
print(A.intersection(B, C)) # frozenset({30})
print(A.intersection()) # frozenset({40, 10, 20, 30})
A = frozenset([10, 20, 30, 40]) # frozenset
B = {10, 30, 50} # set
C = {30, 40} # set
print(A & B) # frozenset({10, 30})
print(A & C) # frozenset({40, 30})
print(A & B & C) # frozenset({30})
print(A) # frozenset({40, 10, 20, 30})
intersection_update() can return the zero or more common elements of the set and *others
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
-
intersection_update()
doesn't create a copy. -
intersection_update()
doesn't exist for a frozenset. -
&=
with or without&
can dointersection_update()
, creating a copy and supportingset
andfrozenset
.
<Set>:
A = {10, 20, 30, 40} # set
B = frozenset([10, 30, 50]) # frozenset
C = frozenset([30, 40]) # frozenset
A.intersection_update(B)
print(A)
# {10, 30}
A.intersection_update(C)
print(A)
# {30}
A.intersection_update(B, C)
print(A)
# {30}
A.intersection_update()
print(A)
# {30}
A = {10, 20, 30, 40} # set
B = frozenset([10, 30, 50]) # frozenset
C = frozenset([30, 40]) # frozenset
A &= B
print(A)
# {10, 30}
A &= C
print(A)
# {30}
A &= B & C
print(A)
# {30}
<Frozenset>:
A = frozenset([10, 20, 30, 40]) # frozenset
B = {10, 30, 50} # set
C = {30, 40} # set
A &= B
print(A)
# frozenset({10, 30})
A &= C
print(A)
# frozenset({30})
A &= B & C
print(A)
# frozenset({30})
Top comments (0)