Python中关于set的基本用法

作者:YKenan 时间:2021-09-23 12:45:39 

1. set 的基本内容

1.基本特点

  • (1) 无序性

  • (2) 确定性

  • (3) 不重复性

2.set() 实质

内部进行 可迭代性的 for 循环

例子:

Python中关于set的基本用法

2. set 的基本方法

2.1 set 的普通基本方法

2.1.1 增

add(self, *args, **kwargs)
copy(self, *args, **kwargs)
# 1. 增

# Add an element to a set. This has no effect if the element is already present.
s = {1, 12, 32, "涟漪", "hello"}
s.add("good")
s.add(32)
print(s)

# Add an element to a set. This has no effect if the element is already present.
s = {1, 12, 32, "涟漪", "hello"}
c = s.copy()
print(c)

结果:

Python中关于set的基本用法

2.1.1 删

clear(self, *args, **kwargs)
pop(self, *args, **kwargs)
remove(self, *args, **kwargs)
discard(self, *args, **kwargs)
# 2. 删

# Remove all elements from this set.
s = {1, 12, 32, "涟漪", "hello"}
s.clear()
print(s)

# Remove and return an arbitrary set element. Raises KeyError if the set is empty.
s = {1, 12, 32, "涟漪", "hello"}
s.pop()
print(s)

# Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError.
s = {1, 12, 32, "涟漪", "hello"}
s.remove(1)
# s.remove("good")
print(s)

# Remove an element from a set if it is a member. If the element is not a member, do nothing.
s = {1, 12, 32, "涟漪", "hello"}
s.discard(1)
s.discard("good")
print(s)

结果:

Python中关于set的基本用法

pop() 是随机删除。

remove() 和 discard() 指定删除,但是指定不存在的元素时,remove() 会报错,而 discard() 不会报错

2.2 set 的逻辑基本方法

2.2.1 set 交集运算

# set 交集运算
x1 = ["a", "b", "c", "d", "e"]
x2 = ["c", "d", "e", "f", "g"]
x3 = []
for x in x1:
   if x in x2:
       x3.append(x)
print(x3)

s_x1 = set(x1)
s_x2 = set(x2)
inter = s_x1.intersection(s_x2)
print(inter)
# 交集符号运算
print(s_x1 & s_x2)
# update
s_x1.intersection_update(s_x2)
print(s_x1)

结果:

Python中关于set的基本用法

2.2.2 set 并集运算

# set 并集运算
x1 = ["a", "b", "c", "d", "e"]
x2 = ["c", "d", "e", "f", "g"]
s_x1 = set(x1)
s_x2 = set(x2)
uni = s_x1.union(s_x2)
print(uni)
# 并集符号运算
print(s_x1 | s_x2)
# update
s_x1.update(s_x2)
print(s_x1)

结果:

Python中关于set的基本用法

2.2.3 set 差集运算

# set 差集运算
x1 = ["a", "b", "c", "d", "e"]
x2 = ["c", "d", "e", "f", "g"]
s_x1 = set(x1)
s_x2 = set(x2)
dif_x1 = s_x1.difference(s_x2)
print(dif_x1)
dif_x2 = s_x2.difference(s_x1)
print(dif_x2)
# 差集符号运算
print(s_x1 - s_x2)
print(s_x2 - s_x1)
# update
s_x1.difference_update(s_x2)
print(s_x1)
s_x2.difference_update(s_x1)
print(s_x2)

结果:

Python中关于set的基本用法

2.2.4 set 对称差集运算

# set 对称差集运算满 * 换律:A△B = B△A
s_x1 = set(x1)
s_x2 = set(x2)
sym = s_x1.symmetric_difference(s_x2)
print(sym)
# 对称差集符号运算
print(s_x1 ^ s_x2)
print(s_x1 - s_x2 | s_x2 - s_x1)
print((s_x1 | s_x2) - (s_x2 & s_x1))
# update
s_x1.symmetric_difference_update(s_x2)
print(s_x1)

结果:

Python中关于set的基本用法

2.2.5 set 逻辑判断运算

# 判断
# Return True if two sets have a null intersection.
x1 = {"a", "b", "c"}
x2 = {"e", "f", "g"}
inter = x1.isdisjoint(x2)
print(inter)

# Report whether another set contains this set.
x1 = {"a", "b", "c"}
x2 = {"a", "b", "c", "e", "f", "g"}
inter = x1.issubset(x2)
print(inter)

# Report whether this set contains another set.
x1 = {"a", "b", "c", "e", "f", "g"}
x2 = {"a", "b", "c"}
inter = x1.issuperset(x2)
print(inter)

结果:

Python中关于set的基本用法

来源:https://blog.csdn.net/YKenan/article/details/88357847

标签:Python,set
0
投稿

猜你喜欢

  • Python 实现Windows开机运行某软件的方法

    2021-05-15 11:48:42
  • python实现简易聊天室(Linux终端)

    2022-03-30 09:44:01
  • Python plt 利用subplot 实现在一张画布同时画多张图

    2021-02-04 02:50:17
  • 利用python将图片转换成excel文档格式

    2022-01-29 03:21:06
  • pytorch 带batch的tensor类型图像显示操作

    2023-06-02 08:47:26
  • JavaScript日期工具类DateUtils定义与用法示例

    2024-04-16 08:51:29
  • Python函数装饰器的使用教程

    2022-10-26 03:01:06
  • Python中matplotlib如何改变画图的字体

    2023-02-19 16:46:38
  • python3使用requests模块爬取页面内容的实战演练

    2022-01-08 18:26:57
  • 如何解决mysql重装失败方法介绍

    2024-01-19 10:52:05
  • 简单form标准化实例——语义结构

    2007-06-20 16:32:00
  • aws 通过boto3 python脚本打pach的实现方法

    2021-09-14 23:33:19
  • python实现自动化群控的步骤

    2023-07-04 11:50:25
  • MySQL乱码问题深层分析

    2009-03-09 14:53:00
  • Python 中random 库的详细使用

    2022-01-19 05:35:14
  • 如何安装并使用conda指令管理python环境

    2022-07-02 04:08:43
  • mac os10.12安装mysql5.7.18教程

    2024-01-19 14:08:27
  • Go 互斥锁和读写互斥锁的实现

    2024-04-25 15:00:41
  • 一个asp版XMLDOM操作类

    2011-04-19 10:50:00
  • line-height 属性的继承问题

    2008-07-26 12:27:00
  • asp之家 网络编程 m.aspxhome.com