织梦CMS - 轻松建站从此开始!

abg欧博官网|登陆|游戏|

当前位置: abg欧博官网|登陆|游戏| > abg欧博 > 文章页

Python3 集合

时间:2025-12-10 10:58来源: 作者:admin 点击: 3 次
Python3 集合 集合(set)是一个无序的不重复元素序列。 集合中的元素不会重复,并且可以进行交集、并集、差集等常见的集合操作。 可以使用大括号 { } 创建集合,元素之间用逗号 , 分隔, 或者也可以使用 set() 函数创建集合。 创建格式: parame = {value01

s.update( "字符串" ) 与 s.update( {"字符串"} ) 含义不同:

s.update( {"字符串"} ) 将字符串添加到集合中,有重复的会忽略。

s.update( "字符串" ) 将字符串拆分单个字符后,然后再一个个添加到集合中,有重复的会忽略。

>>> thisset = set(("Google", "Runoob", "Taobao")) >>> print(thisset) {'Google', 'Runoob', 'Taobao'} >>> thisset.update({"Facebook"}) >>> print(thisset) {'Google', 'Runoob', 'Taobao', 'Facebook'} >>> thisset.update("Yahoo") >>> print(thisset) {'h', 'o', 'Facebook', 'Google', 'Y', 'Runoob', 'Taobao', 'a'} >>>

Nine

dai***0@126.com

8年前 (2018-06-25)

#0

yijiarenppaa

122***9964@qq.com

249

set() 中参数注意事项

1.创建一个含有一个元素的集合

>>> my_set = set(('apple',)) >>> my_set {'apple'}

2.创建一个含有多个元素的集合

>>> my_set = set(('apple','pear','banana')) >>> my_set {'apple', 'banana', 'pear'}

3.如无必要,不要写成如下形式

>>> my_set = set('apple') >>> my_set {'l', 'e', 'p', 'a'} >>> my_set1 = set(('apple')) >>> my_set1 {'l', 'e', 'p', 'a'}

yijiarenppaa

122***9964@qq.com

8年前 (2018-06-30)

#0

Steven Tse

359***491@qq.com

  参考地址

178

集合用 set.pop() 方法删除元素的不一样的感想如下:

1、对于 python 中列表 list、tuple 类型中的元素,转换集合是,会去掉重复的元素如下:

>>> list = [1,1,2,3,4,5,3,1,4,6,5] >>> set(list) {1, 2, 3, 4, 5, 6} >>> tuple = (2,3,5,6,3,5,2,5) >>> set(tuple) {2, 3, 5, 6}

2、集合对 list 和 tuple 具有排序(升序),举例如下:

>>> set([9,4,5,2,6,7,1,8]) {1, 2, 4, 5, 6, 7, 8, 9} >>> set([9,4,5,2,6,7,1,8]) {1, 2, 4, 5, 6, 7, 8, 9}

3、集合的 set.pop() 的不同认为

有人认为 set.pop() 是随机删除集合中的一个元素、我在这里说句非也!对于是字典和字符转换的集合是随机删除元素的。当集合是由列表和元组组成时、set.pop() 是从左边删除元素的如下:

列表实例:

set1 = set([9,4,5,2,6,7,1,8]) print(set1) print(set1.pop()) print(set1)

输出结果:

{1, 2, 4, 5, 6, 7, 8, 9} 1 {2, 4, 5, 6, 7, 8, 9}

元组实例:

set1 = set((6,3,1,7,2,9,8,0)) print(set1) print(set1.pop()) print(set1)

输出结果:

{0, 1, 2, 3, 6, 7, 8, 9} 0 {1, 2, 3, 6, 7, 8, 9}  

Steven Tse

359***491@qq.com

  参考地址

7年前 (2018-08-10)

#0

Sililililidary

282***7697@qq.com

44

>>> thisset = set(("Google", "Runoob", "Taobao", "Facebook")) >>> y=set({'python'}) >>> print(y.union(thisset)) {'python', 'Taobao', 'Google', 'Facebook', 'Runoob'}

输出结果:

{'python', 'Google', 'Taobao', 'Facebook', 'Runoob'}

y 的集合里此时只含有一个元素 'python',而如果不加花括号时,y 的集合里含有'p','y','t','h','o','n'五个元素。

>>> thisset = set(("Google", "Runoob", "Taobao", "Facebook")) >>> y=set('python') >>> print(y.union(thisset)) {'p', 'o', 'y', 'Taobao', 'h', 'Google', 'Facebook', 'Runoob', 'n', 't'}

也可以使用括号:

thisset = set(("Google", "Runoob", "Taobao", "Facebook")) y=set(('python','love')) print(y.union(thisset))

输出结果:

{'Facebook', 'Runoob', 'Taobao', 'python', 'love', 'Google'}

但是当 y 的集合里只有一个字符串时,结果与不加花括号一样。

Sililililidary

282***7697@qq.com

6年前 (2019-11-03)

#0

qinjeremy

qin***emy@msn.com

42

列表的 sort 方法可以实现就地排序(无需创建新对象,字符串按首字母进行排序):

a=[1, 51, 31, -3, 10] a.sort() print(a) s=['a','ab','3e','z'] s.sort() print(s)

输出:

[-3, 1, 10, 31, 51] ['3e', 'a', 'ab', 'z']

按集合中的字符长度进行排序:

a=[1, 51, 31, -3, 10] a.sort() print(a) b=['a','ab','3ae','zaaa','1'] b.sort() print(b) c=['a','ab','3ae','zaaa','1'] c.sort(key=len) print(c)

输出:

[-3, 1, 10, 31, 51] ['1', '3ae', 'a', 'ab', 'zaaa'] ['a', '1', 'ab', '3ae', 'zaaa']

qinjeremy

qin***emy@msn.com

6年前 (2019-11-13)

#0

vipkwd

ser***e@vipkwd.com

93

本课一句话通俗话总结函数:

添加元素

setx.add(string|tuple|bool|number):void setx.update(y [,z...]):void # y、z 为 list|tuple|dict setx.clear():void setx.copy():set # 深拷贝(指向新的内存地址)

删除元素

setx.remove(y):void|KeyError #如删除不存在的元素,有报错 setx.discard(y):void setx.pop():mixed #随机删除集合元素,并返回被删除的元素

右全包含布尔真:判断 setx 集合的所有元素是否都包含在 sety 集合中

setx.issubset(sety):bool >>> x = {"a", "b", "c"} >>> y = {"f", "e", "d", "c", "b", "a"} >>> x.issubset(y) True

左全包含布尔真: 判断 sety 集合的所有元素是否都包含在原始 setx 的集合中

setx.issuperset(sety):bool >>> x = {"a", "b", "c"} >>> y = {"f", "e", "d", "c", "b", "a"} >>> y.issuperset(x) True

右半包含布尔取反:判断集合 sety 中是否有集合 setx 的任一元素(包含返回 False,不包含返回 True)

setx.isdisjoint(sety):bool

合并集合(并集):(注意:集合元素的去重、唯一性)

setx.union(sety [, setz...]):set >>> x = {"a", "b", "c"} >>> y = {"f", "d", "a"} >>> z = {"c", "d", "e"} >>> x.union(y, z) {'c', 'd', 'f', 'e', 'b', 'a'}

左包含差集: 返回集合的差集,即返回的集合元素包含在第一个集合 x 中,但不包含在第二个集合 y(方法的参数)中

setx.difference(sety):set

左引用删除交集(无返回值,即直接修改原内存指向)

setx.difference_update(sety):void >>> x,y ({'banana', 'cshit', 'vipkwd.com', 'alipay'}, {'google', 'runoob', 'facebook', 'alipay'})>>> type(x.difference_update(y)) <class 'NoneType'> >>> x,y ({'banana', 'cshit', 'vipkwd.com'}, {'google', 'runoob', 'facebook', 'alipay'})

左右差集:返回两个集合中不重复的元素集合,即会移除两个集合中都存在的元素

setx.symmetric_difference(sety):set

左引用删除交集且引用追加右差集(引用操作,无返回值)

setx.symmetric_difference_update(sett):void

左右交集:返回两个或更多集合中都包含的元素,即交集

setx.intersection(sety[, seyz....]):set

左引用交集

setx.intersection_update(sety):void

辅助理解:所有提到 “引用” 俩字的都是函数内部直接操作内存指向,故无返回值;反之:如果一个函数没有实质返回值,那么它一定是在函数内部改变了其他位置内容, 否则这个函数就是 dog shit,因为它不具有实质的意义。

vipkwd

ser***e@vipkwd.com

5年前 (2020-09-05)

(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2025-12-14 13:12 最后登录:2025-12-14 13:12
栏目列表
推荐内容