学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