分享

Python集合详解

 网摘文苑 2022-12-13 发布于新疆

前言

在上一篇《Python元组与字典用法详解》中,我们介绍了元组和字典的用法,简单回顾一下:

元组:

  • 创建元组:( )和tuple()函数两种方式

字典:

  • 创建字典:{ }和dict()函数两种方式
  • 访问字典元素:dictname[key]、dictname.get(key[,default])
  • 字典添加键值对:dictname[key] = value
  • 更新字典:dictname.update({'key': value})
  • 删除字典键值对:pop() 用来删除指定的键值对,而popitem() 用来随机删除一个键值对
  • 深拷贝与浅拷贝:浅拷贝在原字典内容发生变更后也会跟着变更,深拷贝会生成一个新的内存地址、及时原有字典内容发生变更也不受影响

而今天的主题是Python集合,Python 中的集合,是一种无序的、可变的序列,和数学中的集合概念一样,用来保存不重复的元素,即集合中的元素都是唯一的,互不相同。形式上看,和字典类似,Python 集合会将所有元素放在一对大括号 {} 中,相邻元素之间用“,”分隔,如下所示:

{element1,element2,...,elementn}

一、创建集合

1.使用 {} 创建

在 Python 中,创建 set 集合可以像列表、元素和字典一样,直接将集合赋值给变量,其语法格式如下:

setname = {element1,element2,...,elementn}

set1 = {1, 2, 3, 4, 5, 6}

2.set()函数创建集合

set() 函数为 Python 的内置函数,其功能是将字符串、列表、元组、range 对象等可迭代对象转换成集合。该函数的语法格式如下:

setname = set(iteration)

# 使用set()方法创建list1 = [5, 6, 7, 8, 9]tup1 = ('a', 'b', 'c')set2 = set(list1)  # 将列表转换为集合set3 = set(tup1)  # 将元组转换为集合,转换结果是无序的print(set2)  # {5, 6, 7, 8, 9}print(set3)  # {'a', 'c', 'b'}

二、向集合中添加元素

set 集合中添加元素,可以使用 set 类型提供的 add() 方法实现,该方法的语法格式为:

setname.add(element)

# 向集合中添加元素set1.add('e') # add()方法添加元素print(set1) # {1, 2, 3, 4, 5, 6, 'e'}set1.update(tup1) # update()方法添加元素print(set1) # {1, 2, 3, 4, 5, 6, 'c', 'e', 'b', 'a'}

三、从集合中删除元素

删除现有 set 集合中的指定元素,可以使用 remove() 方法,该方法的语法格式如下:

setname.remove(element)

# 集合删除元素set1.remove('a')  # 删除指定元素print(set1)  # {1, 2, 3, 4, 5, 6, 'e', 'b', 'c'}set1.pop()  # 从最开始位置删除元素print(set1)  # {2, 3, 4, 5, 6, 'c', 'b', 'e'}set1.discard('e')  # 删除指定元素,不存在则不做任何操作print(set1)  # {2, 3, 4, 5, 6, 'c', 'b'}set1.clear()  # 清空集合print(set1)  # set()

四、清空集合

清空现有 set 集合中的所有元素,可以使用 clear() 方法,该方法的语法格式如下:

setname.clear()

list1 = [1, 3, 4, 5, 6, 6]set2 = set(list1)print(set2) # {1, 3, 4, 5, 6}set2.clear()print(set2) # set()

五、集合求交集、并集、差集

文章图片1

1.通过python运算符

运算操作

Python运算符

含义

例子

交集

&

取两集合公共的元素

set1 & set2 >> [3]

并集

|

取两集合全部的元素

set1 | set2 >> [1,2,3,4,5]

差集

-

取一个集合中另一个集合没有的元素

set1 - set2 >> [1,2]

set2 - set1 >> [4,5]

对称差集

^

取集合A和B中不属于A&B的元素

set1 ^ set2 >> [1,2,4,5]

2.通过函数

方法名

语法格式

含义

difference()

set3 = set1.difference(set2)

将set1中有而set2中没有的元素赋给set3

intersection()

set3 = set1.intersection(set2)

取set1和set2的交集,赋给set3

union()

set3 = set1.difference(set2)

取set1和set2的并集,赋给set3

示例:

# 集合交集、并集、差集set_a = {'a', 'b', 'c', 'd'}set_b = {'c', 'd', 'e', 'f'}# 求交集ict = set_a.intersection(set_b)  # intersection()方法print(ict)  # {'c', 'd'}set_ict = set_a & set_b  # 使用 & 运算符print(set_ict)  # {'c', 'd'}# 求并集union = set_a.union(set_b)  # union()方法print(union)  # {'f', 'd', 'e', 'b', 'c', 'a'}set_union = set_a | set_b  # 使用 | 运算符print(set_union)  # {'f', 'c', 'b', 'd', 'a', 'e'}# 求差集diff = set_a.difference(set_b)  # 使用difference()方法print(diff)  # {'b', 'a'}  set_diff = set_a - set_b  # 使用 - 运算符print(set_diff)  # {'a', 'b'}

六、集合其他用法

1.查看集合所有方法

通过 dir(set) 命令可以查看它有哪些方法:

>>> dir(set)['add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']

2.复制集合

# 复制集合set4 = {1, 3, 5, 7, 9}set5 = set4.copy()print(set5)  # {1, 3, 5, 7, 9}

3.列表去重

# 列表去重list2 = [1, 1, 1, 3, 4, 5, 6, 6]set6 = set(list2)print(set6) # {1, 3, 4, 5, 6}list3 = list(set6)print(list3) # [1, 3, 4, 5, 6]

总结

列表和元组都是有序序列,而字典、集合都是无序序列,不能通过索引(index)获取元素;

列表、字典、集合都是可变序列,而元组是不可变序列,一旦创建,元素就不能发生变化;

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多