脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python 集合 并集、交集 Series list set 转换的实例

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

2021-02-26 00:37远涉江湖 Python

今天小编就为大家分享一篇python 集合 并集、交集 Series list set 转换的实例。具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

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 之后,就可以求解两个集合的 交集、并集关系了

如下:

?
1
2
3
4
5
6
7
8
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 集合 并集、交集 Series list set 转换的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/YAJUN0601/article/details/62422945

延伸 · 阅读

精彩推荐