##########python集合#########

1.集合的定义

集合是一个无序的,不重复的数据组合。

python 集合

• 集合里面不能定义列表

python 集合


2.set的应用场景
• 列表去重
• 关系测试:如交集、差集、并集的关系测试

3.集合的关系测试操作
• 交集  list_1.intersection(list_2)
• 并集  list_1.union(list_2)
• 差集  list_1.difference(list_2)  list_2.difference(list_1)
• 对等差分 list_1.symmetric_difference(list_2)
• 子集 list_1.issubset(list_2)
• 父集 list_1.issuperset(list_2)

• 有无交集 list_1.isdisjoint(list_2)

python 集合


• 交集  list_1 & list_2
• 并集  list_1 | list_2
• 差集  list_1 - list_2  list_2 - list_1

• 对等差分 list_1 ^ list_2

python 集合


4.集合的添加
•s.add(1)

在集合中添加一项

python 集合

•s.update([1,3,4])

在集合中添加多项,跟的参数应该是可迭代类型

python 集合


5.集合的删除
•s.remove(1)

删除集合中指定的元素

python 集合

• s.pop()

随机删除集合中的某个元素,并返回删除的元素集合的其他操作

python 集合


6.集合的其他操作
• len(s)

显示集合set的长度

python 集合

•"1" in s

检测某元素是否为集合s的成员,返回布尔值集合的其他操作

python 集合

• s.copy()

集合的浅拷贝,此处不深入研究,后面会说

python 集合

•s.clear()

清空集合的所有元素

python 集合


相关文章:

  • 2021-10-30
  • 2021-11-18
  • 2021-08-08
  • 2021-12-20
猜你喜欢
  • 2021-09-08
  • 2021-12-10
  • 2021-05-17
  • 2021-07-07
相关资源
相似解决方案