# 1. set concept:

A set is an unordered, non repetitive data set. Its elements are hashable (immutable type), but the set itself is not hashable (so the set cannot be the key of the dictionary). Here are the two most important points of the collection:

1. De duplication. If you change a list into a collection, it will be automatically de duplicated.

2. Relationship test: test the intersection, difference, union and other relationships between two sets of data.

# 2. Collection creation:

```set1 = set({1,2,3,4,2,3,'dabai'})
print(set1)# {1, 2, 3, 4, 'dabai'}```

# 3. Aggregate increase

```#Add (out of order)
set1 = {'Great white','Xiao Bai','alex'}
print(set1)# {'Xiao Bai', 'Great white', 'taibai', 'alex'}

#update() does not need to be added iteratively
set1.update('abc')
print(set1)# {'c', 'b', 'Xiao Bai', 'alex', 'Great white', 'a'}```

# 4. Set delete

```#Pop (random deletion with return value)
set1 = {'Great white','Xiao Bai','alex'}
set1.pop()print(set1)# {'alex', 'big white'}

#Remove (delete by element)
set1 = {'Great white','Xiao Bai','alex'}
set1.remove('Xiao Bai')
print(set1)# {'Great white', 'alex'}```
`#clear(Empty set)set1.clear()print(set1)# set()`
`del set1print(set1)#Delete collection`

# 5. Gather other operations

```#intersection & or intersectionset1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1.intersection(set2))# {4, 5}
print(set1 & set2)# {4, 5}

#Union  | or union
set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 | set2)# {1, 2, 3, 4, 5, 6, 7, 8}
print(set1.union(set2))# {1, 2, 3, 4, 5, 6, 7, 8}

#Anti intersection ^ or symmetric_difference
set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 ^ set2)# {1, 2, 3, 6, 7, 8}
print(set1.symmetric_difference(set2))# {1, 2, 3, 6, 7, 8}

#Difference set (unique) - or difference
print(set1 - set2)#{1, 2, 3}
print(set1.difference(set2))#{1, 2, 3}

#Subset, superset
set1 = {1,2,3}
set2 = {1,2,3,4,5,6}

print(set1 < set2) # True
print(set1.issubset(set2))  # The two are the same set1 yes set2 Subset.

print(set2 > set1) # True
print(set2.issuperset(set1))  # The two are the same set2 yes set1 Superset.```

# 6. frozenset immutable set, let the set become immutable type.

```li = [1, 2, 33, 4, 5, 6]
s = frozenset(li)
print(s,type(s))#frozenset({1, 2, 33, 4, 5, 6}) <class 'frozenset'>```

Keywords: Python

Added by hemantraijain on Thu, 07 May 2020 18:09:37 +0300