多列表相同元素删除

学Python,用RPA

艺赛旗RPA2020.1版本 正在免费下载使用中,欢迎下载使用

www.i-search.com.cn/index.html?from=line1

前言
最近遇到这样一个例子:有多份文档,每份文档里有支行,日期,内容三个数据,现在另一个数据存在一个列表里,把出现几个数据相同的剔除掉。

例:
a = [“ 111”,“ 2”,“ 3”,“ 4”,“ 5”,“ 111”,“ 2”]#支行b = [“ AAA”,“ B”,“ C”,“ D ”,“ E”,“ AAA”,“ B”]#日期c = [“ aaa”,“ b”,“ c”,“ d”,“ e”,“ aaa”,“ k”]#内容

结果:
[‘2’,‘3’,‘4’,‘5’,‘2’] [‘B’,‘C’,‘D’,‘E’,‘B’] [‘b’,‘c’ ,“ d”,“ e”,“ k”]

这里在和朋友讨论下出了三种方法,分享一下:

print("--------------------------方法二--------------------------------------")
def delete_repeat(list_a,list_b,list_c):
from collections import defaultdict
res_a, res_b, res_c = [], [], []
dic = defaultdict(list)
for index, group in enumerate(zip(list_a, list_b, list_c)):
dic[group].append(index)
for key in dic:
if len(dic[key]) == 1:
res_a.append(list_a[dic[key][0]])
res_b.append(list_b[dic[key][0]])
res_c.append(list_c[dic[key][0]])
return res_a, res_b, res_c

def delete_repeat_another_way(list_a,list_b,list_c):
_set = set([])
groups =list(zip(list_a, list_b, list_c))
res = []
for group in groups:
if groups.count(group) == 1:
res.append(group)
res_a,res_b,res_c=[sub_res[0] for sub_res in res],[sub_res[1] for sub_res in res
], [sub_res[2] for sub_res in res]
return res_a, res_b, res_c

a = [“111”, “2”, “3”, “4”, “5”, “111”, “2”] # 支行
b = [“AAA”, “B”, “C”, “D”, “E”, “AAA”, “B”] # 日期
c = [“aaa”, “b”, “c”, “d”, “e”, “aaa”, “k”] # 内容

a1, b1, c1 = delete_repeat_another_way(a, b, c)
print(a1)
print(b1)
print(c1)

print("----------------------方法三---------------------------")

def delete_repeat_final_way(list_a,ist_b,list_c):
from collections import Counter
result_dict = Counter(zip(list_a,list_b,list_c))
res = [i for i in result_dict if result_dict[i] == 1]
res_a,res_b,res_c = [sub_res[0] for sub_res in res
],[sub_res[1] for sub_res in res

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值