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

如下:

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')

相关推荐