[Python 3] [fragmented ideas] set type, set and frozenset

1. Preface

  • This article mainly records the usage of class set([iterable]) and class frozenset([iterable]) constructors
  • The main reason is that when using Python 3, you often check the usage of sets. However, the online data are scattered and there are no examples, so you can sort it out yourself to facilitate quick reference when using Python 3 later

2. Introduction

  • Set object is an unordered multinomial set composed of hashable objects, and the elements in the multinomial set are not repeated
  • Uses: member detection, removing duplicates, set class calculation in Mathematics (intersection, union, difference, etc.)
  • The collection does not support index, slice, or other sequence class operations
  • There are two built-in collection types: set and frozenset. Among them, set type is variable and frozenset is immutable
  • Since the set type is mutable and has no hash value, it cannot be used as a dictionary key or other collection element
  • The frozenset type is immutable and is a hashable object, so it can be used as a key of a dictionary or an element of other collections

3. Common operation

3.1 operation for set and frozenset

3.1.1 create set and frozenset – set() frozenset()

  1. When creating code, remember to look at the code comments
# Create an empty set
empty_set = set()
# Create a non empty set
non_empty_set1 = {1, 2, 3}
non_empty_set2 = set([1, 2, 3, 4])
# Inspection results and types
print(f'empty_set type: {type(empty_set)} \nconteent are {empty_set}')
print(f'non_empty_set1 type: {type(non_empty_set)} \nconteent are {non_empty_set}')
print(f'non_empty_set2 type: {type(non_empty_set2)} \nconteent are {non_empty_set2}')

# Create an empty frozenset
# Because frozenset is immutable, you cannot create an empty set
empty_frozenset = frozenset()
# Create a non empty frozenset
non_empty_frozenset1 = frozenset([1, 2, 3, 4])
non_empty_frozenset2 = frozenset(non_empty_set1)

# Inspection results and types
print(f'empty_frozenset type: {type(empty_frozenset)} \nconteent are {empty_frozenset}')
print(f'non_empty_frozenset1 type: {type(non_empty_frozenset1)} \nconteent are {non_empty_frozenset1}')
print(f'non_empty_frozenset2 type: {type(non_empty_frozenset2)} \nconteent are {non_empty_frozenset2}')
  1. Operation results
empty_set type: <class 'set'> 
conteent are set()
non_empty_set1 type: <class 'set'> 
conteent are {1, 2, 3}
non_empty_set2 type: <class 'set'> 
conteent are {1, 2, 3, 4}
empty_frozenset type: <class 'frozenset'> 
conteent are frozenset()
non_empty_frozenset1 type: <class 'frozenset'> 
conteent are frozenset({1, 2, 3, 4})
non_empty_frozenset2 type: <class 'frozenset'> 
conteent are frozenset({1, 2, 3})

3.1.2 get the number of elements in the set - len(s)

  1. code
# Gets the number of elements in the set
print(f"non_empty_set1 length: {len(non_empty_set1)}" )
  1. result
non_empty_set1 length: 3

3.1.3 test whether x is a member of s – x in s

  1. code
# Gets the number of elements in the set
print(f"1 in non_empty_set2? {1 in non_empty_set2}" )
print(f"10 in non_empty_set2? {10 in non_empty_set2}" )
print(f"non_empty_set1 in non_empty_set2? {non_empty_set1 in non_empty_set2} \n")
  1. result
1 in non_empty_set2? True
10 in non_empty_set2? False
non_empty_set1 in non_empty_set2? False 

3.1.4 judge whether two sets have intersection - isdisjoint(other)

  1. code
# False if there is intersection and True if there is no intersection
non_empty_set3 = {5, 6, 7}
non_empty_set4 = {4, 5, 6, 7}
print(f"non_empty_set3 and non_empty_set2 No intersection? { non_empty_set2.isdisjoint(non_empty_set3)}" )
print(f"non_empty_set4 and non_empty_set2 No intersection? { non_empty_set2.isdisjoint(non_empty_set4)}" )
  1. result
non_empty_set3 and non_empty_set2 Have intersection? True
non_empty_set4 and non_empty_set2 Have intersection? False

3.1.5 judge that set A is A subset of set B – A.issubset(B)

  1. code
# Yes subset returns True, non subset returns False
print(f"non_empty_set1 yes non_empty_set2 Subset of? { non_empty_set1.issubset(non_empty_set2)}" )
print(f"non_empty_set1 yes non_empty_set2 Subset of?  { non_empty_set1 <= non_empty_set2}" )
print(f"non_empty_set1 yes non_empty_set2 True subset of?  { non_empty_set1 < non_empty_set2}" )
  1. result
non_empty_set1 yes non_empty_set2 Subset of? True
non_empty_set1 yes non_empty_set2 Subset of?  True
non_empty_set1 yes non_empty_set2 True subset of?  True

3.1.6 union of set A and set B – A.union(B)

  1. code
### 3.1.6 finding the union of set a and set B -- A.union(B)
print(f"non_empty_set4 and non_empty_set2 The union of is? { non_empty_set4.union(non_empty_set2)}" )
print(f"non_empty_set4 and non_empty_set2 The union of is? { non_empty_set4 | non_empty_set2}" )
  1. result
non_empty_set4 and non_empty_set2 The union of is? {1, 2, 3, 4, 5, 6, 7}
non_empty_set4 and non_empty_set2 The union of is? {1, 2, 3, 4, 5, 6, 7}

3.1.7 finding the intersection of set a and set B – A.intersection(B)

  1. code
### 3.1.7 finding the intersection of set a and set B -- A.intersection(B)
print(f"non_empty_set4 and non_empty_set2 The intersection of is? { non_empty_set4.intersection(non_empty_set2)}" )
print(f"non_empty_set4 and non_empty_set2 The intersection of is? { non_empty_set4 & non_empty_set2}" )
  1. result
non_empty_set4 and non_empty_set2 The intersection of is? {4}
non_empty_set4 and non_empty_set2 The intersection of is? {4}

3.1.8 finding the difference set between set a and set B – A.difference(B)

  1. code
### 3.1.8 find the difference set between set a and set B -- A.difference(B)
print(f"non_empty_set4 and non_empty_set2 The difference set is? { non_empty_set4.difference(non_empty_set2)}" )
print(f"non_empty_set4 and non_empty_set2 The difference set is? { non_empty_set4 - non_empty_set2}" )
  1. result
non_empty_set4 and non_empty_set2 The difference set is? {5, 6, 7}
non_empty_set4 and non_empty_set2 The difference set is? {5, 6, 7}

3.1.9 finding the minus intersection of set A and set B – A.symmetric_difference(B)

  1. code
### 3.1.9 find the result of subtraction intersection of set A and set B -- A.symmetric_difference(B)
print(f"non_empty_set4 and non_empty_set2 Minus intersection is? { non_empty_set4.symmetric_difference(non_empty_set2)}" )
  1. result
non_empty_set4 and non_empty_set2 Minus intersection is? {1, 2, 3, 5, 6, 7}

3.1.0 shallow copy of set A – B = A.copy()

  1. Shallow copy is to copy only the parent object and not the child object. For details, see this article: Python direct assignment, shallow copy and deep copy parsing
  2. code
### 3.1.0 shallow copy of set A -- B = A.copy()
print(f"non_empty_set2 The shallow copy of is: {non_empty_set2.copy()}" )
  1. result
non_empty_set2 The shallow copy of is: {1, 2, 3, 4}

4. References

  1. pyrhon 3.7.12 Development Manual: built in types
  2. Python direct assignment, shallow copy and deep copy parsing

Tags: Python python3

Posted on Sun, 28 Nov 2021 16:27:34 -0500 by mike0193