DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Set & Frozenset functions in Python (3)

Buy Me a Coffee

*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.
  • intersection() creates a copy.
  • & does intersection(), creating a copy and supporting set and frozenset.

<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}
Enter fullscreen mode Exit fullscreen mode
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}
Enter fullscreen mode Exit fullscreen mode

<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})
Enter fullscreen mode Exit fullscreen mode
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})
Enter fullscreen mode Exit fullscreen mode

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.
  • intersection_update() doesn't create a copy.
  • intersection_update() doesn't exist for a frozenset.
  • &= with or without & can do intersection_update(), creating a copy and supporting set and frozenset.

<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}
Enter fullscreen mode Exit fullscreen mode
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}
Enter fullscreen mode Exit fullscreen mode

<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})
Enter fullscreen mode Exit fullscreen mode

Top comments (0)