我的代码的目标是,在我正在解析的文件中,最多只能有一个具有相同名称和生日的人。

这是我称为ind的词典中的词典:

{I19: {'BIRT': '13 FEB 1981', 'sex': 'M', 'id': 'I19', 'family': 'F23', 'name': 'Dick /Smith/'}}
{I32: {'BIRT': '27 MAY 1991', 'sex': 'M', 'id': 'I32', 'family': 'F16', 'name': 'Nick /Tary/'}}
{I30: {'BIRT': '3 SEP 1993', 'sex': 'F', 'id': 'I30', 'family': 'F16', 'name': 'Mary /Test/'}}
{I26: {'BIRT': '2 JUN 1983', 'sex': 'F', 'id': 'I26', 'family': 'F23', 'name': 'Jane /Smith/'}}
{I01: {'name': 'Joe /Smith/', 'family': 'F23', 'BIRT': '15 JUL 1960', 'sex': 'M', 'id': 'I01', 'DEAT': '31 DEC 2013'}}
{I07: {'BIRT': '23 SEP 1960', 'sex': 'F', 'id': 'I07', 'family': 'F23', 'name': 'Jennifer /Smith/'}}
{I19: {'BIRT': '13 FEB 1981', 'sex': 'M', 'id': 'I19', 'family': 'F23', 'name': 'Dick /Smith/'}}


我的代码应该删除Dick Smith的条目之一,因为其中有2个。

到目前为止,这是我这部分的代码(尚未删除重复项):

for individual in ind:
    name1 = ind[individual]['name']
    bdate1 = ind[individual]['BIRT']
    for individual_2 in ind:
        name2 = ind[individual]['name']
        bdate2 = ind[individual]['BIRT']
        if name1 == name2 and bdate1 == bdate2:
            print("{} already exists. Removing duplicate entry.".format(name1))


但这给了我:

Dick /Smith/ already exists. Removing duplicate entry.
Dick /Smith/ already exists. Removing duplicate entry.
Dick /Smith/ already exists. Removing duplicate entry.
Dick /Smith/ already exists. Removing duplicate entry.
Dick /Smith/ already exists. Removing duplicate entry.
Dick /Smith/ already exists. Removing duplicate entry.
Nick /Tary/ already exists. Removing duplicate entry.
Nick /Tary/ already exists. Removing duplicate entry.
Nick /Tary/ already exists. Removing duplicate entry.
Nick /Tary/ already exists. Removing duplicate entry.
Nick /Tary/ already exists. Removing duplicate entry.
Nick /Tary/ already exists. Removing duplicate entry.
Mary /Test/ already exists. Removing duplicate entry.
Mary /Test/ already exists. Removing duplicate entry.
Mary /Test/ already exists. Removing duplicate entry.
Mary /Test/ already exists. Removing duplicate entry.
Mary /Test/ already exists. Removing duplicate entry.
Mary /Test/ already exists. Removing duplicate entry.
Jane /Smith/ already exists. Removing duplicate entry.
Jane /Smith/ already exists. Removing duplicate entry.
Jane /Smith/ already exists. Removing duplicate entry.
Jane /Smith/ already exists. Removing duplicate entry.
Jane /Smith/ already exists. Removing duplicate entry.
Jane /Smith/ already exists. Removing duplicate entry.
Joe /Smith/ already exists. Removing duplicate entry.
Joe /Smith/ already exists. Removing duplicate entry.
Joe /Smith/ already exists. Removing duplicate entry.
Joe /Smith/ already exists. Removing duplicate entry.
Joe /Smith/ already exists. Removing duplicate entry.
Joe /Smith/ already exists. Removing duplicate entry.
Jennifer /Smith/ already exists. Removing duplicate entry.
Jennifer /Smith/ already exists. Removing duplicate entry.
Jennifer /Smith/ already exists. Removing duplicate entry.
Jennifer /Smith/ already exists. Removing duplicate entry.
Jennifer /Smith/ already exists. Removing duplicate entry.
Jennifer /Smith/ already exists. Removing duplicate entry.


道歉,如果问题看起来很简单-我是新手。将不胜感激。

最佳答案

list_of_dict = [{'I19': {'BIRT': '13 FEB 1981', 'sex': 'M', 'id': 'I19', 'family': 'F23', 'name': 'Dick /Smith/'}},
     {'I32': {'BIRT': '27 MAY 1991', 'sex': 'M', 'id': 'I32', 'family': 'F16', 'name': 'Nick /Tary/'}}
,{'I30': {'BIRT': '3 SEP 1993', 'sex': 'F', 'id': 'I30', 'family': 'F16', 'name': 'Mary /Test/'}}
,{'I26': {'BIRT': '2 JUN 1983', 'sex': 'F', 'id': 'I26', 'family': 'F23', 'name': 'Jane /Smith/'}}
,{'I01': {'name': 'Joe /Smith/', 'family': 'F23', 'BIRT': '15 JUL 1960', 'sex': 'M', 'id': 'I01', 'DEAT': '31 DEC 2013'}}
,{'I07': {'BIRT': '23 SEP 1960', 'sex': 'F', 'id': 'I07', 'family': 'F23', 'name': 'Jennifer /Smith/'}}
,{'I19': {'BIRT': '13 FEB 1981', 'sex': 'M', 'id': 'I19', 'family': 'F23', 'name': 'Dick /Smith/'}}]

new_d = {v['name'] : {k : v} for d in list_of_dict for k,v in d.items()}

for v in new_d.values():
    print(v)


输出

{'I19': {'BIRT': '13 FEB 1981', 'sex': 'M', 'id': 'I19', 'family': 'F23', 'name': 'Dick /Smith/'}}
{'I32': {'BIRT': '27 MAY 1991', 'sex': 'M', 'id': 'I32', 'family': 'F16', 'name': 'Nick /Tary/'}}
{'I30': {'BIRT': '3 SEP 1993', 'sex': 'F', 'id': 'I30', 'family': 'F16', 'name': 'Mary /Test/'}}
{'I26': {'BIRT': '2 JUN 1983', 'sex': 'F', 'id': 'I26', 'family': 'F23', 'name': 'Jane /Smith/'}}
{'I01': {'name': 'Joe /Smith/', 'family': 'F23', 'BIRT': '15 JUL 1960', 'sex': 'M', 'id': 'I01', 'DEAT': '31 DEC 2013'}}
{'I07': {'BIRT': '23 SEP 1960', 'sex': 'F', 'id': 'I07', 'family': 'F23', 'name': 'Jennifer /Smith/'}}


请注意,在此隐式中,dup时仅保存姓氏

关于python - 如何从字典中删除字典中的重复条目?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50900608/

10-10 12:28