字典拼音

2024-05-13 02:43:06 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,那么。。。 我下面有些词典不想合作。 字典“a”代表我们人力资源数据库中存储的数据。 字典“b”表示存储在AS400 iSeries数据中的数据。 注意:这些不是真值 目标是使用至少2个字段将“a”中的键与“b”中的键匹配,同时仍然能够引用该键

换言之,在匹配2个值之后,我希望能够创建一个新字典,将a的键与b的键匹配:

match_dictionary[*key from a*] = (*key from b*)
a.pop(*key from a*)
b.pop(*key from b*)

代码如下:

import collections
a = {'ada123456' : ('123','adam','jones') , 'jus567890':('567','justin','brady') , 'mul345678':('345','muller','thomas')}
b = {'ADAMJ' : ('123','jones') , 'JBRADY':('justin','brady') , 'THOMASM':('345','muller','thomas')}

if [i for i in [(x[1],x[2]) for x in [a[c] for c in a.keys()]]] in [b[d] for d in b.keys()]:
    print('Why won\'t this work?')

if ('justin','brady') in [b[d] for d in b.keys()]:
    print('\nthis works though')

if ('justin','brady') in [(x[1],x[2]) for x in [a[c] for c in a.keys()]]:
    print('\nthis too')

Tags: 数据keyinfromforif字典thomas
1条回答
网友
1楼 · 发布于 2024-05-13 02:43:06

我把它作为一个列表的关键,以防你有多个匹配

new_dict = defaultdict(list)
for b_name, b_values in b.items():
    for a_name, a_values in a.items():
        intersect = set(a_values).intersection(set(b_values))
        if len(intersect) >= 2:
            new_dict[a_name].append(b_name)

但要把它改成

            new_dict[a_name] = b_name

如果你愿意的话。那么你就不需要默认的dict了

这将产生:

defaultdict(list,
            {'ada123456': ['ADAMJ'],
             'jus567890': ['JBRADY'],
             'mul345678': ['THOMASM']})

如果你想要一个正常的dict回来,只要把它包在dict()

相关问题 更多 >