*Memo:
- My post explains set and frozenset functions (1).
- My post explains set and frozenset functions (3).
- My post explains set and frozenset functions (4).
- My post explains a set (1).
- My post explains a frozenset (1).
union() can return all the 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
-
union()
creates a new set or frozenset. -
|
can dounion()
, supportingset
andfrozenset
.
<Set>:
A = {10, 50} # set
B = frozenset([10, 30, 50]) # frozenset
C = [20, 40] # list
print(A.union(B)) # {50, 10, 30}
print(A.union(C)) # {40, 10, 50, 20}
print(A.union(B, C)) # {50, 20, 40, 10, 30}
print(A.union()) # {10, 50}
A = {10, 50} # set
B = frozenset([10, 30, 50]) # frozenset
C = frozenset([20, 40]) # frozenset
print(A | B) # {50, 10, 30}
print(A | C) # {40, 10, 50, 20}
print(A | B | C) # {50, 20, 40, 10, 30}
print(A) # {10, 50}
<Frozenset>:
A = frozenset([10, 50]) # frozenset
B = {10, 30, 50} # set
C = [20, 40] # list
print(A.union(B)) # frozenset({50, 10, 30})
print(A.union(C)) # frozenset({40, 10, 50, 20})
print(A.union(B, C)) # frozenset({50, 20, 40, 10, 30})
print(A.union()) # frozenset({10, 50})
A = frozenset([10, 50]) # frozenset
B = {10, 30, 50} # set
C = {20, 40} # set
print(A | B) # frozenset({50, 10, 30})
print(A | C) # frozenset({40, 10, 50, 20})
print(A | B | C) # frozenset({50, 20, 40, 10, 30})
print(A) # frozenset({10, 50})
update() can add zero or more iterables to the set 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
-
update()
doesn't create a new set. -
update()
doesn't exist for a frozenset. -
|=
with or without|
can doupdate()
, supportingset
andfrozenset
.
<Set>:
A = {10, 30, 50}
print(A)
# {10, 50, 30}
A.update({20, 30, 40}) # set
A.update(frozenset([20, 30, 40])) # frozenset
A.update([20, 30, 40]) # list
A.update({20, 30, 40}, frozenset([20, 30, 40]), [20, 30, 40])
# set # frozenset # list
print(A)
# {50, 20, 40, 10, 30}
A.update()
print(A)
# {50, 20, 40, 10, 30}
A = {10, 30, 50}
print(A)
# {10, 50, 30}
A |= {20, 30, 40} # set
A |= {20, 30, 40} | frozenset([20, 30, 40]) | frozenset([20, 30, 40])
# set # frozenset # frozenset
print(A)
# {50, 20, 40, 10, 30}
<Frozenset>:
A = frozenset([10, 30, 50])
print(A)
# frozenset({10, 50, 30})
A |= frozenset([20, 30, 40]) # frozenset
A |= frozenset([20, 30, 40]) | {20, 30, 40} | {20, 30, 40}
# frozenset # set # set
print(A)
# frozenset({50, 20, 40, 10, 30})
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 new set or frozenset. -
&
doesintersection()
, 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 new set. -
intersection_update()
doesn't exist for a frozenset. -
&=
with or without&
can dointersection_update()
, 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)