博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python学习之set集合方法总结
阅读量:5867 次
发布时间:2019-06-19

本文共 5971 字,大约阅读时间需要 19 分钟。

 

一、定义

set是一个无序且不重复的元素集合。

集合对象是一组无序排列的可哈希的值,集合成员可以做字典中的键。集合支持用in和not in操作符检查成员,由len()内建函数得到集合的基数(大小), 用 for 循环迭代集合的成员。但是因为集合本身是无序的,不可以为集合创建索引或执行切片(slice)操作,也没有键(keys)可用来获取集合中元素的值。

set和dict一样,只是没有value,相当于dict的key集合,由于dict的key是不重复的,且key是不可变对象因此set也有如下特性:

  1. 不重复

  2. 元素为不可变对象

二、创建

s = set()s = {
11,22,33,44} #注意在创建空集合的时候只能使用s=set(),因为s={}创建的是空字典
a=set('boy')b=set(['y', 'b', 'o','o']) c=set({ "k1":'v1','k2':'v2'}) d={ 'k1','k2','k2'} e={('k1', 'k2','k2')} print(a,type(a)) print(b,type(b)) print(c,type(c)) print(d,type(d)) print(e,type(e)) OUTPUT: { 'o', 'b', 'y'} 
{ 'o', 'b', 'y'}
{ 'k1', 'k2'}
{ 'k1', 'k2'}
{('k1', 'k2', 'k2')}

三、基本操作

比较

se = {
11, 22, 33}be = {
22, 55} temp1 = se.difference(be) #找到se中存在,be中不存在的集合,返回新值 print(temp1) #{33, 11} print(se) #{33, 11, 22} temp2 = se.difference_update(be) #找到se中存在,be中不存在的集合,覆盖掉se print(temp2) #None print(se) #{33, 11},

删除

discard()、remove()、pop()

se = {
11, 22, 33}se.discard(11)se.discard(44) # 移除不存的元素不会报错 print(se) se = { 11, 22, 33} se.remove(11) se.remove(44) # 移除不存的元素会报错 print(se) se = { 11, 22, 33} # 移除末尾元素并把移除的元素赋给新值 temp = se.pop() print(temp) # 33 print(se) # {11, 22}

取交集

se = {
11, 22, 33}be = {
22, 55} temp1 = se.intersection(be) #取交集,赋给新值 print(temp1) # 22 print(se) # {11, 22, 33} temp2 = se.intersection_update(be) #取交集并更新自己 print(temp2) # None print(se) # 22

判断

se = {
11, 22, 33}be = {
22}print(se.isdisjoint(be)) #False,判断是否不存在交集(有交集False,无交集True) print(se.issubset(be)) #False,判断se是否是be的子集合 print(se.issuperset(be)) #True,判断se是否是be的父集合

合并

se = {
11, 22, 33}be = {
22}temp1 = se.symmetric_difference(be) # 合并不同项,并赋新值 print(temp1) #{33, 11} print(se) #{33, 11, 22} temp2 = se.symmetric_difference_update(be) # 合并不同项,并更新自己 print(temp2) #None print(se) #{33, 11}

取并集

se = {11, 22, 33}be = {22,44,55}temp=se.union(be)   #取并集,并赋新值print(se)       #{33, 11, 22}print(temp)     #{33, 22, 55, 11, 44}

更新

se = {11, 22, 33}be = {22,44,55}se.update(be)  # 把se和be合并,得出的值覆盖seprint(se)se.update([66, 77])  # 可增加迭代项print(se)

集合的转换

se = set(range(4))li = list(se)tu = tuple(se)st = str(se) print(li,type(li)) print(tu,type(tu)) print(st,type(st)) OUTPUT: [0, 1, 2, 3] 
(0, 1, 2, 3)
{ 0, 1, 2, 3}

四、源码

class set(object):    """ set() -> new empty set object set(iterable) -> new set object Build an unordered collection of unique elements. """ def add(self, *args, **kwargs): """添加""" """ Add an element to a set. This has no effect if the element is already present. """ pass def clear(self, *args, **kwargs): """清除""" """ Remove all elements from this set. """ pass def copy(self, *args, **kwargs): """浅拷贝""" """ Return a shallow copy of a set. """ pass def difference(self, *args, **kwargs): """比较""" """ Return the difference of two or more sets as a new set. (i.e. all elements that are in this set but not the others.) """ pass def difference_update(self, *args, **kwargs): """ Remove all elements of another set from this set. """ pass def discard(self, *args, **kwargs): """删除""" """ Remove an element from a set if it is a member. If the element is not a member, do nothing. """ pass def intersection(self, *args, **kwargs): """ Return the intersection of two sets as a new set. (i.e. all elements that are in both sets.) """ pass def intersection_update(self, *args, **kwargs): """ Update a set with the intersection of itself and another. """ pass def isdisjoint(self, *args, **kwargs): """ Return True if two sets have a null intersection. """ pass def issubset(self, *args, **kwargs): """ Report whether another set contains this set. """ pass def issuperset(self, *args, **kwargs): """ Report whether this set contains another set. """ pass def pop(self, *args, **kwargs): """ Remove and return an arbitrary set element. Raises KeyError if the set is empty. """ pass def remove(self, *args, **kwargs): """ Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError. """ pass def symmetric_difference(self, *args, **kwargs): """ Return the symmetric difference of two sets as a new set. (i.e. all elements that are in exactly one of the sets.) """ pass def symmetric_difference_update(self, *args, **kwargs): """ Update a set with the symmetric difference of itself and another. """ pass def union(self, *args, **kwargs): """ Return the union of sets as a new set. (i.e. all elements that are in either set.) """ pass def update(self, *args, **kwargs): """ Update a set with the union of itself and others. """ pass def __and__(self, *args, **kwargs): """ Return self&value. """ pass def __contains__(self, y): """ x.__contains__(y) <==> y in x. """ pass def __eq__(self, *args, **kwargs): """ Return self==value. """ pass def __getattribute__(self, *args, **kwargs): """ Return getattr(self, name). """ pass def __ge__(self, *args, **kwargs): """ Return self>=value. """ pass def __gt__(self, *args, **kwargs): """ Return self>value. """ pass def __iand__(self, *args, **kwargs): """ Return self&=value. """ pass def __init__(self, seq=()): # known special case of set.__init__ """ set() -> new empty set object set(iterable) -> new set object Build an unordered collection of unique elements. # (copied from class doc) """ pass def __ior__(self, *args, **kwargs): """ Return self|=value. """ pass def __isub__(self, *args, **kwargs): """ Return self-=value. """ pass def __iter__(self, *args, **kwargs): """ Implement iter(self). """ pass def __ixor__(self, *args, **kwargs): """ Return self^=value. """ pass def __len__(self, *args, **kwargs): """ Return len(self). """ pass def __le__(self, *args, **kwargs): """ Return self<=value. """ pass def __lt__(self, *args, **kwargs): """ Return self

转载于:https://www.cnblogs.com/Ronaldo2011/p/9417612.html

你可能感兴趣的文章
基于Kafka实现分布式事件驱动
查看>>
送给 Java 程序员的 Spring 学习指南\n
查看>>
分布式团队面临的五大问题及解决办法
查看>>
Micronaut教程:如何使用基于JVM的框架构建微服务
查看>>
避免标准数据模型
查看>>
用WinForm/WPF代码来为.NET Core 3.0功能投票
查看>>
如何使用 Druid 和 Kafka 构造 Kappa 架构完成流量分析
查看>>
腾讯游戏云聚焦全球化布局,集合行业能力建立游戏云生态
查看>>
小米人员架构调整:组建中国区,王川任总裁
查看>>
MongoDB数据库工具dbKoda 1.0版本提供了更好的用户体验和性能实验室
查看>>
算法(第4版) Chapter 4.1 无向图
查看>>
从零开始搭建论坛(三):Flask框架简单介绍
查看>>
手机端页面自适应解决方案—rem布局(进阶版,附源码示例)
查看>>
Spring AOP的实现原理
查看>>
IP通信中音频编解码技术与抗丢包技术概要
查看>>
Python 自定义函数的特殊属性(收藏专用)
查看>>
Laravel学习笔记之Demo2——用Ajax来做个ToDoList(待更新)
查看>>
破解Mac上changes提示输入序列号
查看>>
【总结】Json的转换与传递
查看>>
安全无小事:如何给企业用户数据上把锁?
查看>>