Python中关于set的基本用法
作者:YKenan 发布时间:2021-09-23 12:45:39
标签: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)
结果:
来源:https://blog.csdn.net/YKenan/article/details/88357847
0
投稿
猜你喜欢
- 爬虫请求头网页获取:通过urlopen来进行获取requset.urlopen(url,data,timeout)第一个参数url即为URL
- 代码如下:'返回某年总共有多少天 Function DayOfYear(ByVal y) DayOfYear = DatePart(
- 日志日志是跟踪软件运行时所发生的事件的一种方法。软件开发者在代码中调用日志函数,表明发生了特定的事件。事件由描述性消息描述,该描述性消息可以
- 一、基本概念查找(Searching)就是根据给定的某个值,在查找表中确定一个其关键字等于给定值的数据元素(或记录)。查找表(Search
- 主要记录一下:图片验证码1.获取登录界面的图片2.获取验证码位置3.在登录页面截取验证码保存4.调用百度api识别(目前准确率较高的识别图片
- 在SQL Server Management Studio 用WINDOWS连接的情况下改实列的“属性&rdqu
- 1 什么是prototype JavaScript中对象的prototype属性,可以返
- CREATE OR REPLACE PROCEDURE PROC6338196642095312503719(输入新闻主题 Varchar2
- 前言现在最流行的本地存储莫过于 cookie 的应用,但浏览器对 cookie 有很多限制,最大的限制在于其对cookie 总大小
- 一个js用星投票的例子,不是常见的图片版,当然你如果会点js代码的话应该可以改为更加美观的图片投票,原理都一样。本程序只演示了读取星的个数,
- 摘要:下拉菜单经常带来更多的可用性问题,并且常常容易被混淆。这是因为网页设计师们往往会在不同的几种情况下使用它。同时,滚动的菜单降低了网页的
- 先按照下面的表结构创建mysql_order_by_test数据表,我们用实例一点一点告诉你,MySQL order by的用法。ORDER
- 在电子产品的设计中,大家经常提到简洁是设计的重要元素。可是很多产品,不见得简洁就是第一要素。简洁的设计,必须是在对用户需求透彻理解,引导用户
- 元数据简介元数据 (metadata) 最常见的定义为“有关数据的结构数据”,或者再简单一点就是“关于数据的信息”,日常生活中的图例、图书馆
- 前言PHP 中有个释放变量的语句叫做unset(从PHP4开始unset已经不再是一个函数了,而是一个语句),本文主要给大家介绍了关于php
- 继续flask的学习之旅。今天介绍flask的登陆管理模块,还记得上一篇中的blog小项目么,登录是咱们自己写的验证代码,大概有以下几个步骤
- 分离结构与表现的另一个重要方面是使用语义化的标记来构造文档内容。一个 XHTML 元素的存在就意味被标记内容的那部分有相应的结构化的意义,没
- IIS上设置301 跳转相信大家都会,只要在网站-属性-主目录里 选择重定向URL就行了,这样整站就跳转到目标站点了,但是有个问
- 本文实例讲述了python3 BeautifulSoup模块使用字典的方法抓取a标签内的数据。分享给大家供大家参考,具体如下:# -*- c
- [前言]:搭往公司的班车,遇到其他部门的同事,他问了很多关于我的工作的问题,由此引发这篇文章。这些问题,我也经常被其他人问到,其中既有我们亲