目录
删除 --- setname.remove(element)
集合是一个无序不有重复的序列
用于关系测试和消除重复值
使用 { } 框定元素,使用逗号分隔
- setname = { value1,value2,value3,...,valuen}
-
- set1 = {1, 2, 3, 4, 5}
- set2 = {'name', 'age', 'num', 'score'}
- set3 = set('命运给予我们的不是失望之酒,而是机会之杯')
- set4 = set([1.2, 3.4, 5.6])
- set5 = set() # 空集合,不能为{}
- set6 = set('apple', ) # 必须增加逗号
- print(set1, '\n', set2, '\n', set3, '\n', set4, '\n', set5, '\n', set6, '\n', )
- # 集合本质为无序的可变序列,输出时顺序可能不同
-
- 结果:
- {1, 2, 3, 4, 5}
- {'age', 'score', 'name', 'num'}
- {'失', '机', '予', '命', '不', '望', '会', '杯', '们', '的', '运', '之', '我', ',', '是', '酒', '给', '而'}
- {1.2, 3.4, 5.6}
- set()
- {'l', 'p', 'a', 'e'}
- list1 = [1, 2, 1, 5, 4, 8, 1, 8, 1, 2, 9, 19, 1, 1, 1, 8, 91, 5, 1, 1, 1]
- print(list1)
- set1 = set(list1)
- print(set1)
-
- 结果:
- [1, 2, 1, 5, 4, 8, 1, 8, 1, 2, 9, 19, 1, 1, 1, 8, 91, 5, 1, 1, 1]
- {1, 2, 4, 5, 8, 9, 19, 91}
- set1 = set(['java', 'c', 'go', 'php'])
- set1.add('python')
- print(set1)
-
- 结果:
- {'php', 'c', 'python', 'java', 'go'}
- set1 = set(['java', 'c', 'go', 'php'])
- set1.add('python')
- print(set1)
- set1.remove('java')
- print(set1)
-
- 结果:
- {'go', 'java', 'php', 'c', 'python'}
- {'go', 'php', 'c', 'python'}
- set1 = set('abcdefg')
- set2 = set('efghijk')
- # 交集
- print(set1 & set2)
- # 并集
- print(set1 | set2)
- # 差集
- print(set1 - set2)
- # 对称差集
- print(set1 ^ set2)
-
- 结果:
- {'f', 'e', 'g'}
- {'a', 'j', 'i', 'd', 'k', 'f', 'h', 'c', 'b', 'e', 'g'}
- {'a', 'c', 'b', 'd'}
- {'a', 'j', 'i', 'c', 'b', 'd', 'k', 'h'}
-
- Process finished with exit code 0
符合 --- == !=
返回值 --- Ture False
- A = {1, 2, 3, 4}
- B = {3, 4, 5, 6}
- C = {1, 2, 3, 4}
- print(A == B, A == C)
-
- 结果:
- False True
sidisjoint( ) --- 两个集合没有共同元素时返回Ture
- A = {1, 2, 3, 4}
- B = {5, 6, 7, 8}
-
- print(A.isdisjoint(B))
-
- 结果:
- True
issubset() --- 测试是否为子集
- A = {1, 2, 3, 4}
- B = {5, 6, 7, 8}
-
- print(A.issubset(B))
-
- 结果:
- False
issuperset( ) --- 测试是否为父集
- A = {1, 2, 3, 4}
- B = {1, 2, 3, 4, 5, 6, 7, 8}
-
- print(A.issuperset(B))
-
- 结果:
- False
- update( ) --- 将集合加到另一个集合中
-
- A = {1, 2, 3, 4}
- B = { 5, 6, 7, 8}
- A.update(B)
- print(A )
-
- 结果:
- {1, 2, 3, 4, 5, 6, 7, 8}
difference_update( ) --- 删除集合中重复的元素
- A = {1, 2, 3, 4}
- B = {3, 4, 5, 6, 7, 8}
- A.difference_update(B)
- print(A)
-
- 结果:
- {1, 2}
定义 --- set是可变数据类型,frozenset是不可变的数据类型集合称为冻结集合,定义好内容后不可修改、删除、操作参照元组
- x = frozenset([1, 2, 3])
- y = frozenset([5, 7, 9])
- print(x, y)
- print(x & y, x | y)
- print(x[0]) #报错
-
- 结果:
- frozenset({1, 2, 3}) frozenset({9, 5, 7})
- frozenset() frozenset({1, 2, 3, 5, 7, 9})
- Traceback (most recent call last):
- File "E:\pythontext\test3.py", line 2112, in
- print(x[0])
- TypeError: 'frozenset' object is not subscriptable
dir()用于查找方法名
- print(dir(list))
-
- 结果:
- ['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
数据结构 | 是否可变 | 是否重复 | 是否有序 | 定义符号 |
---|---|---|---|---|
list | 可变 | 可重复 | 有序 | [ ] |
tuple | 不可变 | 可重复 | 有序 | ( ) |
dict | 可变 | 可重复 | 无序 | { key : value} |
set | 可变 | 不可重复 | 无序 | { } |