字典维恩图的散列或字典?
Hashing or dictionaries for Venn diagram of dictionaries?
我有两本字典(每本 80K 个词条)都具有相同的格式:
A= {'1':'a', '2':'b', '3':'c', '4':'d'}
B= {'2':'b', '3':'a', '4':'d', '5':'e'}
Dict_A 中的一些 key:value 对与 Dict_B 重叠。将这些词典想象成维恩图圆圈。我的目标是找到这两个词典的重叠部分并生成新的词典:Dict_A、Dict_B、Dict_C 和 Dict_D。
我想最终得到 6 部词典:
其中
原始词典:
A= {'1':'a', '2':'b', '3':'c', '4':'d'}
B= {'2':'b', '3':'a', '4':'d', '5':'e'}
Dict_A:具有 A
唯一键的项目
Dict_A = {'1':'a'}
Dict_B:具有 B
唯一键的项目
Dict_B = {'5':'e'}
Dict_C:A 和 B 共享键但值不同的项目
Dict_C = {'3':'a', '3':'c'}
Dict_D:具有 A 和 B 共享的键和值的项。
Dict_D = {'2':'b', '4':'d'}
我知道如何通过集合来求出这些东西的长度:
shared1 = set(A.items()) & set(B.items())
shared2 = set(A.keys()) & set(B.keys())
然后我可以找出Dict_C中应该有多少:
len(shared2) - len(shared1)
但我不知道如何制作它们的字典。
由于字典查找时间很快 (O(1)),您可以使用字典理解:
>>> # use iteritems on Python2
>>> shared1 = {k: v for k, v in A.items() if i in B}
Dict_A: items with keys unique to A
Dict_B: items with keys unique to B
>>> Dict_A = {k: v for k,v in A.items() if k not in B}
>>> Dict_B = {k: v for k,v in B.items() if k not in A}
为了您以后的任务,例如:
Dict_C: items with keys shared by A and B, but with values that are different
您可以使用列表理解:
>>> # use iteritems for Python2
>>> # list comprehension
>>> shared1 = [k for k, v in A.items() if B.get(k) == v]
>>> # set comprehension
>>> shared1 = {k for k, v in A.items() if B.get(k) == v}
请记住,键必须是唯一的,您只需要 键 不同:列表(或集合)就足够了(使用花括号或集合理解, 生成一个集合).
Dict_D: items with keys AND values shared by A and B.
使用与上述相同的思路即可
>>> Dict_D = {k: v for k, v in A.items() if B.get(k) == v}
我有两本字典(每本 80K 个词条)都具有相同的格式:
A= {'1':'a', '2':'b', '3':'c', '4':'d'}
B= {'2':'b', '3':'a', '4':'d', '5':'e'}
Dict_A 中的一些 key:value 对与 Dict_B 重叠。将这些词典想象成维恩图圆圈。我的目标是找到这两个词典的重叠部分并生成新的词典:Dict_A、Dict_B、Dict_C 和 Dict_D。 我想最终得到 6 部词典: 其中
原始词典:
A= {'1':'a', '2':'b', '3':'c', '4':'d'}
B= {'2':'b', '3':'a', '4':'d', '5':'e'}
Dict_A:具有 A
唯一键的项目Dict_A = {'1':'a'}
Dict_B:具有 B
唯一键的项目Dict_B = {'5':'e'}
Dict_C:A 和 B 共享键但值不同的项目
Dict_C = {'3':'a', '3':'c'}
Dict_D:具有 A 和 B 共享的键和值的项。
Dict_D = {'2':'b', '4':'d'}
我知道如何通过集合来求出这些东西的长度:
shared1 = set(A.items()) & set(B.items())
shared2 = set(A.keys()) & set(B.keys())
然后我可以找出Dict_C中应该有多少: len(shared2) - len(shared1)
但我不知道如何制作它们的字典。
由于字典查找时间很快 (O(1)),您可以使用字典理解:
>>> # use iteritems on Python2
>>> shared1 = {k: v for k, v in A.items() if i in B}
Dict_A: items with keys unique to A
Dict_B: items with keys unique to B
>>> Dict_A = {k: v for k,v in A.items() if k not in B}
>>> Dict_B = {k: v for k,v in B.items() if k not in A}
为了您以后的任务,例如:
Dict_C: items with keys shared by A and B, but with values that are different
您可以使用列表理解:
>>> # use iteritems for Python2
>>> # list comprehension
>>> shared1 = [k for k, v in A.items() if B.get(k) == v]
>>> # set comprehension
>>> shared1 = {k for k, v in A.items() if B.get(k) == v}
请记住,键必须是唯一的,您只需要 键 不同:列表(或集合)就足够了(使用花括号或集合理解, 生成一个集合).
Dict_D: items with keys AND values shared by A and B.
使用与上述相同的思路即可
>>> Dict_D = {k: v for k, v in A.items() if B.get(k) == v}