*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 a set (1).
- My post explains a frozenset (1).
isdisjoint() can check if the set or frozenset and other
don't have any common elements as shown below:
*Memo:
- The 1st argument is
other
(Required-Type:Iterable):- Don't use
other=
.
- Don't use
<Set>:
A = {10, 20, 30} # set
B = frozenset([40, 50]) # frozenset
C = [30, 40] # list
print(A.isdisjoint(B)) # True
print(A.isdisjoint(C)) # False
<Frozenset>:
A = frozenset([10, 20, 30]) # frozenset
B = {40, 50} # set
C = [30, 40] # list
print(A.isdisjoint(B)) # True
print(A.isdisjoint(C)) # False
issubset() can check if every element in the set or frozenset is in other
as shown below:
*Memo:
- The 1st argument is
other
(Required-Type:Iterable):- Don't use
other=
.
- Don't use
-
<=
can doissubset()
, supportingset
andfrozenset
.
<Set>:
A = {10, 20, 30} # set
B = frozenset([10, 20, 30, 40]) # frozenset
C = [10, 20, 30] # list
D = (10, 20) # tuple
print(A.issubset(B)) # True
print(A.issubset(C)) # True
print(A.issubset(D)) # False
A = {10, 20, 30} # set
B = frozenset([10, 20, 30, 40]) # frozenset
C = frozenset([10, 20, 30]) # frozenset
D = frozenset([10, 20]) # frozenset
print(A <= B) # True
print(A <= C) # True
print(A <= D) # False
<Frozenset>:
A = frozenset([10, 20, 30]) # frozenset
B = {10, 20, 30, 40} # set
C = [10, 20, 30] # list
D = (10, 20) # tuple
print(A.issubset(B)) # True
print(A.issubset(C)) # True
print(A.issubset(D)) # False
A = frozenset([10, 20, 30]) # frozenset
B = {10, 20, 30, 40} # set
C = {10, 20, 30} # frozenset
D = {10, 20} # frozenset
print(A <= B) # True
print(A <= C) # True
print(A <= D) # False
issuperset() can check if every element in other
is in the set or frozenset as shown below:
*Memo:
- The 1st argument is
other
(Optional-Type:Iterable):- Don't use
other=
.
- Don't use
-
>=
can doissuperset()
, supportingset
andfrozenset
.
<Set>:
A = {10, 20, 30} # set
B = frozenset([10, 20, 30, 40]) # frozenset
C = [10, 20, 30] # list
D = (10, 20) # tuple
print(A.issuperset(B)) # False
print(A.issuperset(C)) # True
print(A.issuperset(D)) # True
A = {10, 20, 30} # set
B = frozenset([10, 20, 30, 40]) # frozenset
C = frozenset([10, 20, 30]) # frozenset
D = frozenset([10, 20]) # frozenset
print(A >= B) # False
print(A >= C) # True
print(A >= D) # True
<Frozenset>:
A = frozenset([10, 20, 30]) # frozenset
B = {10, 20, 30, 40} # set
C = [10, 20, 30] # list
D = (10, 20) # tuple
print(A.issuperset(B)) # False
print(A.issuperset(C)) # True
print(A.issuperset(D)) # True
A = frozenset([10, 20, 30]) # frozenset
B = {10, 20, 30, 40} # set
C = {10, 20, 30} # set
D = {10, 20} # set
print(A >= B) # False
print(A >= C) # True
print(A >= D) # True
sorted() can convert a set or frozenset to a list, then sort the list, then the sorted list is converted to a set or frozenset with set() or frozenset() respectively as shown below:
*Memo:
- The 1st argument is
iterable
(Required-Type:Iterable):- Don't use
iterable=
.
- Don't use
- The 2nd argument is
key
(Optional-Default:None
-Type:Callable or NoneType). - The 3rd argument is
reverse
(Optional-Default:False
-Type:bool
) to reverse the list. -
sorted()
creates a new list:- Be careful,
sorted()
does shallow copy instead of deep copy as my issue.
- Be careful,
<Set>:
A = {3, 5, -2, 1, -4}
print(sorted(A))
print(sorted(A, key=None, reverse=False))
# [-4, -2, 1, 3, 5]
print(set(sorted(A)))
# {1, 3, 5, -4, -2}
print(set(sorted(A, reverse=True)))
# {1, 3, 5, -4, -2}
print(set(sorted(A, key=abs)))
# {1, 3, 5, -4, -2}
print(set(sorted(A, key=abs, reverse=True)))
# {1, 3, 5, -4, -2}
A = {"apple", "Banana", "Kiwi", "cherry"}
""" Case sensitive sort """
print(set(sorted(A)))
# {'Banana', 'Kiwi', 'cherry', 'apple'}
""" Case insensitive sort """
print(set(sorted(A, key=str.upper)))
print(set(sorted(A, key=str.lower)))
# {'Banana', 'Kiwi', 'cherry', 'apple'}
""" Sort by the length of a word """
print(set(sorted(A, key=len)))
# {'Banana', 'Kiwi', 'cherry', 'apple'}
<Frozenset>:
A = frozenset([3, 5, -2, 1, -4])
print(sorted(A))
print(sorted(A, key=None, reverse=False))
# [-4, -2, 1, 3, 5]
print(frozenset(sorted(A)))
# frozenset({1, 3, 5, -4, -2})
print(frozenset(sorted(A, reverse=True)))
# frozenset({1, 3, 5, -4, -2})
print(frozenset(sorted(A, key=abs)))
# frozenset({1, 3, 5, -4, -2})
print(frozenset(sorted(A, key=abs, reverse=True)))
# frozenset({1, 3, 5, -4, -2})
A = frozenset(["apple", "Banana", "Kiwi", "cherry"])
""" Case sensitive sort """
print(frozenset(sorted(A)))
# frozenset({'Banana', 'Kiwi', 'cherry', 'apple'})
""" Case insensitive sort """
print(frozenset(sorted(A, key=str.upper)))
print(frozenset(sorted(A, key=str.lower)))
# frozenset({'Banana', 'Kiwi', 'cherry', 'apple'})
""" Sort by the length of a word """
print(frozenset(sorted(A, key=len)))
# frozenset({'Banana', 'Kiwi', 'cherry', 'apple'})
Top comments (0)