Python中关于set的基本用法

 更新时间:2023年04月25日 09:01:35   作者:YKenan  
这篇文章主要介绍了Python中关于set的基本用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

1. set 的基本内容

1.基本特点

  • (1) 无序性
  • (2) 确定性
  • (3) 不重复性

2.set() 实质

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

例子:

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)

结果:

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)

结果:

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)

结果:

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)

结果:

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)

结果:

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)

结果:

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函数生成器原理及使用详解

    Python函数生成器原理及使用详解

    这篇文章主要介绍了Python函数生成器原理及使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • Python深入学习之装饰器

    Python深入学习之装饰器

    这篇文章主要介绍了Python深入学习之装饰器,装饰器(decorator)是一种高级Python语法,本文全面介绍了Python中的装饰器,需要的朋友可以参考下
    2014-08-08
  • Django一小时写出账号密码管理系统

    Django一小时写出账号密码管理系统

    这篇文章主要介绍了Django一小时写出账号密码管理系统,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • python csv实时一条一条插入且表头不重复问题

    python csv实时一条一条插入且表头不重复问题

    这篇文章主要介绍了python csv实时一条一条插入且表头不重复问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • python实现记事本功能

    python实现记事本功能

    这篇文章主要为大家详细介绍了python实现记事本功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • python让列表倒序输出的实例

    python让列表倒序输出的实例

    今天小编就为大家分享一篇python让列表倒序输出的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-06-06
  • python numpy和list查询其中某个数的个数及定位方法

    python numpy和list查询其中某个数的个数及定位方法

    今天小编就为大家分享一篇python numpy和list查询其中某个数的个数及定位方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-06-06
  • 基于Python实现植物大战僵尸游戏的示例代码

    基于Python实现植物大战僵尸游戏的示例代码

    植物大战僵尸是一款经典的塔防类游戏,玩家通过种植各种植物来抵御僵尸的攻击,本文将详细介绍如何使用Python和Pygame库来实现一个简单的植物大战僵尸游戏,文中通过代码示例讲解的非常详细,感兴趣的小伙伴跟着小编一起来看看吧
    2024-10-10
  • python机器学习库常用汇总

    python机器学习库常用汇总

    这篇文章主要为大家汇总了常用python机器学习库,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11
  • Python设计模式之抽象工厂模式原理与用法详解

    Python设计模式之抽象工厂模式原理与用法详解

    这篇文章主要介绍了Python设计模式之抽象工厂模式,简单讲述了抽象工厂模式的概念、原理并结合实例形式分析了Python实现与使用抽象工厂模式的相关操作技巧,需要的朋友可以参考下
    2019-01-01

最新评论