Reference Python series serialization from scratch, by Wang Dawei Python enthusiast community
Refer to Hellobi Live | 1 hour icebreaker introduction to Python
Refer to "Concise Python Tutorial"
Note: For more serialization, please see [python]
content
gather
Features of Collection Elements
- Elemental certainty
- mutuality of elements
- disorder of elements
A set is the same as a dictionary, the order in it is unordered, that is to say, {1,2,3} and {3,2,1} are equal, and the elements of the set cannot be repeated, that is to say: {1,2, 3,3} does not exist, it should be written as {1,2,3}, the elements in the set need to be immutable types, you can use numbers, strings, tuples but not lists, dictionaries as element values , and The keys in the dictionary are the same. So the set can be understood as a dictionary with only keys and no values .
1 Creation of a collection
1.1 Create directly with braces {}
1) Set elements are immutable types, so you can use values, strings, tuples, but not lists , dictionaries as element values
set1 = {1,2,3,4,5}#直接使用大括号{}创建
print(set1)
print(type(set1))
set2 = {1,2.33,'Python',(1,'b')}
print(set2)
print(type(set2))
The result is
{1, 2, 3, 4, 5}
<class 'set'>
{1, 2.33, (1, 'b'), 'Python'}
<class 'set'>
2) If you write a duplicate value in the collection when you create it, no error will be reported, but according to the mutuality, only one will be saved:
set3 = {1,2,2,3,4}#如果你创建时在集合中写了重复的值,不会报错,但根据互异性,只会保存一个
The result is
{1, 2, 3, 4}
1.2 Creating an empty set with set()
set4 = set()#创建空集合
set4
The result is
set()
1.3 Create with lists or tuples
set5 = set([1,2,3,4,5,6])#使用列表或者元组创建,在set()里放入list或者tuple可以创建集合,该集合的元素就是列表或元组的元素
print (set5)
set6 = set((1,2,3,4,5))
print (set6)
The result is
{1, 2, 3, 4, 5, 6}
{1, 2, 3, 4, 5}
1.4 Creating with Strings
Created with a string, the elements of the resulting collection are each character of the string
set7 = set('Python')#使用字符串创建,得到的集合的元素是字符串的每个字符
set7
The result is
{'P', 'h', 'n', 'o', 't', 'y'}
2 methods of collection
Just like lists, dictionaries, and tuples, there are many ways to operate on sets
2.1 add()
set.add( x ), add element x to the set
set8 = {1,2,3,4,5}
set8.add('a')#向集合中添加元素a
set8
The result is
{1, 2, 3, 4, 5, 'a'}
2.2 update()
set.update(a_set), use the set a_set to update the original set, which is to give the elements of the set in parentheses to set
set9 = {'b','c'}
set8.update(set9)#使用集合set9更新原集合,就是把set9集合的元素给set8
set8
The result is
{1, 2, 3, 4, 5, 'a', 'b', 'c'}
2.3 pop()
set.pop( ), removes and returns any element in the set
set8 = {1, 2, 3, 4, 5, 'a', 'b', 'c'}
set8.pop()#删除并且返回集合中的任意元素
print (set8)
set8.pop()#这里的删除是不能指定删除谁的,和列表的不一样
print (set8)
The result is
{2, 3, 4, 5, 'b', 'c', 'a'}
{3, 4, 5, 'b', 'c', 'a'}
Note: The last element is deleted by default in the list, the specified key-value pair can be deleted in the dictionary, and the set is deleted randomly
2.4 remove()
Remove element x from the set, and report an error if x does not exist
set8 = {3, 4, 5, 'b', 'c', 'a'}
set8.remove(3)#删除集合中的元素3,如果3不存在就报错
set8
The result is
{4, 5, 'a', 'b', 'c'}
set8.remove(1)
result error
KeyError: 1
2.5 discard()
set.discard(x), delete element x in the set, do nothing if x does not exist
set8 = {4, 5, 'b', 'c', 'a'}
set8.discard(1)#删除集合中的元素1,如果1不存在则什么也不做
set8
The result is
{4, 5, 'a', 'b', 'c'}
2.6 clear()
set.clear( ), clears all elements in the collection
set8 = {4, 5, 'a', 'b', 'c'}
set8.clear()#清空集合中的所有元素
set8
The result is
set()
3 Mathematical operations on sets
Speaking of collections, how can this classic Venn diagram be omitted!
set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print (set1.intersection(set2)) #求交集
print (set1.union(set2)) #求并集
print (set1.difference(set2)) #作差
The result is
{4, 5}
{1, 2, 3, 4, 5, 6, 7, 8}
{1, 2, 3}
4 Determine if an element is in a set
set1 = {1, 2, 3, 4, 5}
print (1 in set1)#判断元素是否在集合中
print (10 in set1)
The result is
True
False
5 Determine whether set2 is a subset of set1
set1 = {1,2,3,4,5}
set2 = {1,2,3}
print (set2.issubset(set1)) #判断set2是否是set1的子集
set3 = {0,1,2}
print (set3.issubset(set1))
The result is
True
False
Note: For more serialization, please see [python]