0

我的脚本是这样的:

import csv
with open('lees.csv','rU') as naver:

    reader = csv.DictReader (naver)
    for alist in reader:
        name = alist["naam"]
        polisnumber = alist["polisnr"]
        riskadr = alist["risico adr"]
        insurencecode = alist["branchecode"]
        relationnumber = alist["rel"]
        header = alist["aanhef"]
        tav = alist["tav"]
        thelist = [name,riskadr,polisnumber,
                  relationnumber,insurencecode,header,tav]

脚本的输出是:

['Cautus  B.V.', 'plein 92', '1129008', '10', 'AVB', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa']
['Cautus  B.V.', 'Wei 9-11', '1019123', '10', 'AVB', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa']
['Cautus  B.V.', 'plein 92', '1129008', '10', 'BEDR', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa']
['Cautus  B.V.', 'Wei 9-11', '1019123', '10', 'BEDR', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa']
['De company', 'tiellaan 42', 'KD0022232', '13', 'AVB', 'Geachte heer Tigch', 'De heer I. Tigch']
['De company', 'tiellaan 42', 'KD0022232', '13', 'DAS', 'Geachte heer Tigch', 'De heer I. Tigch']
['Slever ', 'klopt 42', 'KD2220115', '17', 'AVB', 'Geachte heer Slever', 'De heer T.Slever']

如您所见,我从 .csv 文件创建了一个目录。

我的问题是我需要制作一个脚本来过滤riskadrwei 9-11/ plein 92/ )中的重复项,并将第二个重复tiellaan 42项的insurencecodeAVB/ BEDR/DAS等)riskadr与其他条目一起添加到新列表中的第一个。

所以现在我们有 2 个条目具有相同的风险 adr,如下所示:

['De company', 'tiellaan 42', 'KD0022232', '13', 'AVB', 'Geachte heer Tigch', 'De heer I. Tigch']
['De company', 'tiellaan 42', 'KD0022232', '13', 'DAS', 'Geachte heer Tigch', 'De heer I. Tigch']

但我想要一个 scipt 从这 2 个条目中创建 1 个条目,并将保险类型添加到第一个 1 中,如下所示(AVB/DAS):

['De company', 'tiellaan 42', 'KD0022232', '13', 'AVB','DAS', 'Geachte heer Tigch', 'De heer I. Tigch']
4

3 回答 3

1

您应该能够使用itertools.groupby实现您的目标:

from itertools import groupby

# define input
l = [['Cautus  B.V.', 'plein 92', '1129008', '10', 'AVB', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa'],
     ['Cautus  B.V.', 'Wei 9-11', '1019123', '10', 'AVB', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa'],
     ['Cautus  B.V.', 'plein 92', '1129008', '10', 'BEDR', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa'],
     ['Cautus  B.V.', 'Wei 9-11', '1019123', '10', 'BEDR', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa'],
     ['De company', 'tiellaan 42', 'KD0022232', '13', 'AVB', 'Geachte heer Tigch', 'De heer I. Tigch'],
     ['De company', 'tiellaan 42', 'KD0022232', '13', 'DAS', 'Geachte heer Tigch', 'De heer I. Tigch'],
     ['Slever ', 'klopt 42', 'KD2220115', '17', 'AVB', 'Geachte heer Slever', 'De heer T.Slever']]

# remove clutter
l_clean = [(x[1], x[4]) for x in l]

# sort (groupby requires input to be sorted)
l_sorted = sorted(l_clean)

# group by first column
l_final = [(k, zip(*v)[1]) for k,v in groupby(l_sorted, key=lambda x:x[0])]

# print output
for k,v in l_final: 
    print k, list(v)

输出是:

Wei 9-11 ['AVB', 'BEDR']
klopt 42 ['AVB']
plein 92 ['AVB', 'BEDR']
tiellaan 42 ['AVB', 'DAS']

请注意,您将需要调整key用于排序和分组的函数,以便在输入不同于l_clean.

于 2012-10-17T10:05:28.933 回答
0

你可能需要这些方面的东西。有一个内存数组(ultimatelist),您可以在其中检查类似列表的存在。如果找到附加保险代码

def search(item, array):
    for i in range(len(array)):
        # if first four elements and last two elements are identical
        if array[i][:4] == item[0:4] and array[i][-2:] == item[-2:]:
            return i
    return -1

index = search(thelist, ultimatelist):
if index > 0:
    ultimatelist[index] = ultimatelist[index][:4] + thelist[4] + ultimatelist[index][4:]
于 2012-10-17T10:02:45.380 回答
0
>>> a = [
... ('De company', 'tiellaan 42', 'KD0022232', '13', 'DAS', 'Geachte heer Tigch', 'De heer I. Tigch'),
... ('De company', 'tiellaan 42', 'KD0022232', '13', 'DAS', 'Geachte heer Tigch', 'De heer I. Tigch'),
... ]
>>> 
>>> set(a)
set([('De company', 'tiellaan 42', 'KD0022232', '13', 'DAS', 'Geachte heer Tigch', 'De heer I. Tigch')])
>>>

将它们保存为元组而不是列表,并将它们添加到集合中......如果这是你需要的

于 2012-10-17T09:47:59.247 回答