欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

python 集合 并集、交集 Series list set 转换的实例

时间:2021-04-13 09:11:43|栏目:Python代码|点击:

set转成list方法如下: list转成set方法如下:

s = set('12342212')                                                      l = ['12342212']
 print s    # set(['1', '3', '2', '4'])                                    s = set(l[0])
 l = list(s)                                                                          print s    # set(['1', '3', '2', '4'])
 l.sort()    # 排序                                                               m = ['11','22','33','44','11','22']
 print l    # ['1', '2', '3', '4']                                              print set(m)    # set(['11', '33', '44', '22'])

可见set和lsit可以自由转换,在删除list中多个/海量重复元素时,可以先转换成set,然后再转回list并排序(set没有排序)。此种方法不仅方便且效率较高。

转换成set 之后,就可以求解两个集合的 交集、并集关系了

如下:

AA_16_only, AA15_only 为两个 Series 对象:


AA_16o_list =set(AA_16_only)
 AA15o_list = set(AA15_only)
 AA15_AA_16_only = AA15o_list.intersection(AA_16o_list)
 AA15_AA_16_only = pd.Series(list(AA15_AA_16_only))
 AA15_AA_16_only.to_csv('AA15_AA_16_only.csv')

上一篇:Python随手笔记第一篇(2)之初识列表和元组

栏    目:Python代码

下一篇:Python txt文件加入字典并查询的方法

本文标题:python 集合 并集、交集 Series list set 转换的实例

本文地址:http://www.codeinn.net/misctech/100026.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有