【问题标题】:Combining two dictionaries into one if the value from the one dictionary exist in another如果一个字典中的值存在于另一个字典中,则将两个字典合并为一个
【发布时间】:2021-09-03 13:59:17
【问题描述】:

我正在尝试构建一个名为 author_venues 的字典,其中作者姓名是键,值是他们发布的场所列表。

给了我两本字典: 一个示例 author_pubs 字典,其中作者姓名是键,出版物 ID 列表是值

defaultdict(list,
            {'José A. Blakeley': ['2',
              '25',
              '2018',
              '2185',
              '94602',
              '145114',
              '182779',
              '182780',
              '299422',
              '299426',
              '299428',
              '299558',
              '302125',
              '511816',
              '521294',
              '597967',
              '598123',
              '598125',
              '598130',
              '598132',
              '598134',
              '598136',
              '598620',
              '600180',
              '600221',
              '642049',
              '643606',
              '808458',
              '832249',
              '938531',
              '939047',
              '1064640',
              '1064641',
              '1065929',
              '1118153',
              '1269074',
              '2984279',
              '3154713',
              '3169639',
              '3286099',
              '3494140'],
             'Yuri Breitbart': ['3',
              '4',
              '76914',
              '113875',
              '140847',
              '147900',
              '147901',
              '150951',
              '176221',
              '176896',
              '182963',
              '200336',
              '262940',
              '285098',
              '285564',
              '299526',
              '301313',
              '303418',
              '304160',
              '400040',
              '400041',
              '400174',
              '400175',
              '402178',
              '482506',
              '482785',
              '544757',
              '545233',
              '545429',
              '559737',
              '559761',
              '559765',
              '559783',
              '559785',
              '597889',
              '598201',
              '598202',
              '598203',
              '599325',
              '599899',
              '620806',
              '636455',
              '641884',
              '642157',
              '654200',
              '654201',
              '740600',
              '740602',
              '833336',
              '844280',
              '856032',
              '856222',
              '888870',
              '934979',
              '938228',
              '941484',
              '945339',
              '949548',
              '971592',
              '971593',
              '972813',
              '972958',
              '1064100',
              '1064690',
              '1064691',
              '1064693',
              '1064694',
              '1078369',
              '1078370',
              '1089675',
              '1095084',
              '1121956',
              '1122006',
              '1122610',
              '1127610',
              '1138059',
              '1138061',
              '1141938',
              '1227365',
              '1278703',
              '1319498',
              '2818906',
              '2876867',
              '2978458',
              '3015058',
              '3223418'],

场地名称为键,发布 ID 列表为值的示例场地_pubs 字典

defaultdict(list,
            {'Modern Database Systems': ['2',
              '3',
              '4',
              '5',
              '6',
              '7',
              '8',
              '9',
              '10',
              '11',
              '12',
              '13',
              '14',
              '15',
              '16',
              '17',
              '18',
              '19',
              '20',
              '21',
              '22',
              '23',
              '24',
              '25',
              '26',
              '27',
              '28',
              '29',
              '30',
              '31',
              '32',
              '33',
              '34',
              '1203459',
              '3000615',
              '3000616',
              '3000617',
              '3000618',
              '3000619',
              '3000620',
              '3000621',
              '3000622',
              '3000623',
              '3000624',
              '3000625',
              '3000626'],
             'Object-Oriented Concepts, Databases, and Applications': ['36',
              '37',
              '38',
              '39',
              '40',
              '41',
              '42',
              '43',
              '44',
              '45',
              '46',
              '47',
              '48',
              '49',
              '50',
              '51',
              '52',
              '53',
              '54',
              '55',
              '56',
              '57',
              '58',
              '59'],
             'The INGRES Papers': ['60',
              '61',
              '62',
              '63',
              '64',
              '65',
              '66',
              '67',
              '68',
              '69'],
             'Temporal Databases': ['168',
              '169',
              '170',
              '171',
              '172',
              '173',
              '174',
              '175',
              '176',
              '177',
              '178',
              '179',
              '180',
              '181',
              '182',
              '183',
              '184',
              '185',
              '186',
              '187',
              '188',
              '189',
              '190',
              '627582',
              '627584',
              '627588',
              '627589',
              '627591',
              '627592',
              '627593',
              '627594',
              '627596',
              '627600',
              '627601',
              '627602',
              '627603',
              '627604',
              '627605',
              '627608',
              '627613',
              '627615',
              '627616',
              '627617'],

生成的字典应类似于 {'author':['venue1','venue2','venue3']} author_venue = defaultdict(list)

这是我写的代码:

for k,v in author_pubs.items():
    for item in v:
        for x,y in venue_pubs.items():
            if item in y:
                venue = x
                author_venue[k].append(venue)

但是这个循环需要很长时间,因为我有超过 300 万条记录

请帮忙!

【问题讨论】:

    标签: python list dictionary filter


    【解决方案1】:

    您可以“反转”字典venue_pubs 以加快搜索速度:

    from collections import defaultdict
    
    
    author_pubs = {
        "author1": [1, 2, 3],
        "author2": [3, 4, 5],
    }
    
    venue_pubs = {
        "xxx1": [1, 4, 20],
        "xxx2": [4, 30, 40],
    }
    
    # "invert" dictionary `venue_pubs`:
    tmp = defaultdict(list)
    for k, v in venue_pubs.items():
        for val in v:
            tmp[val].append(k)
    
    
    author_venue = defaultdict(list)
    
    for k, v in author_pubs.items():
        for item in v:
            venues = tmp.get(item)
            if not venues is None:
                author_venue[k].extend(venues)
    
    print(author_venue)
    

    打印:

    defaultdict(<class 'list'>, {'author1': ['xxx1'], 'author2': ['xxx1', 'xxx2']})
    

    编辑:删除重复项:

    # ...
    
    for k in author_venue:
        author_venue[k] = list(set(author_venue[k]))
    
    print(author_venue)
    

    【讨论】:

    • 非常感谢!还有一个问题,我如何只为每个作者保留唯一的场地?
    • @YoungZ 看set() 只需将author_venue 字典中list 的值转换为set 即可
    猜你喜欢
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 2021-03-13
    • 1970-01-01
    相关资源
    最近更新 更多