如何比较 Python 中不同嵌套的字典和列表并找到交集?
How to compare differently nested dictionaries and lists in Python and find intersection?
我现在有两个(或多或少复杂的)列表/字典。第一个包含十六进制的图像名称和图像像素颜色。所以它看起来像这样:
{
0: {'hex': ['#c3d6db', '#c7ccc0', '#9a8f6a', '#8a8e3e'], 'filename': 'imag0'},
1: {'hex': ['#705b3c', '#6a5639', '#442f1e', '#4a3d28'], 'filename': 'img-xyz'},
…
}
所以在这种情况下,我会有 2 张 2 x 2 像素的图像。
第二个字典包含许多十六进制值作为键和一个 id 作为值。看起来像:
{'#b0a7aa': '9976', '#595f5b': '19367', '#9a8f6a': '24095'…}
现在我想做的是查看我的图像(第一个列表)中是否有与第二个列表中的一个匹配的颜色值。如果是这样,那么我想知道第一个列表中的文件名和第二个列表中匹配键的值、id。
我怎样才能做到这一点?
使用 dictionary view objects 在您的 hex
列表和 hex-id 字典之间产生交集:
for entry in images.values():
for key in hexidmap.keys() & entry['hex']:
print('{} {} {}'.format(entry['filename'], key, hexidmap[key]))
&
生成键集和十六进制值列表之间的交集。
以上假设您使用的是Python 3;如果您使用的是 Python 2,请使用 dict.viewkeys()
而不是 .keys()
。
演示:
>>> images = {
... 0: {'hex': ['#c3d6db', '#c7ccc0', '#9a8f6a', '#8a8e3e'], 'filename': 'imag0'},
... 1: {'hex': ['#705b3c', '#6a5639', '#442f1e', '#4a3d28'], 'filename': 'img-xyz'},
... }
>>> hexidmap = {'#b0a7aa': '9976', '#595f5b': '19367', '#9a8f6a': '24095'}
>>> for entry in images.values():
... for key in hexidmap.keys() & entry['hex']:
... print('{} {} {}'.format(entry['filename'], key, hexidmap[key]))
...
imag0 #9a8f6a 24095
for index in d1:
print [(d1[index]["filename"], d2[i], i) for i in d1[index]["hex"] if i in d2]
>>> [('imag0', '24095', '#9a8f6a')]
[]
dicta = {
0: {'hex': ['#c3d6db', '#c7ccc0', '#9a8f6a', '#8a8e3e'], 'filename': 'imag0'},
1: {'hex': ['#705b3c', '#6a5639', '#442f1e', '#4a3d28'], 'filename': 'img-xyz'},
}
dictb = {'#c3d6db': '9976', '#595f5b': '19367', '#9a8f6a': '24095'}
intersection = {}
for o in dicta.values():
intersect = list(set(o['hex']) & set(dictb.keys()))
intersection[o['filename']] = intersect if intersect else "No intersection"
print (intersection)
>>>{'imag0': ['#c3d6db', '#9a8f6a'], 'img-xyz': 'No intersection'}
我现在有两个(或多或少复杂的)列表/字典。第一个包含十六进制的图像名称和图像像素颜色。所以它看起来像这样:
{
0: {'hex': ['#c3d6db', '#c7ccc0', '#9a8f6a', '#8a8e3e'], 'filename': 'imag0'},
1: {'hex': ['#705b3c', '#6a5639', '#442f1e', '#4a3d28'], 'filename': 'img-xyz'},
…
}
所以在这种情况下,我会有 2 张 2 x 2 像素的图像。
第二个字典包含许多十六进制值作为键和一个 id 作为值。看起来像:
{'#b0a7aa': '9976', '#595f5b': '19367', '#9a8f6a': '24095'…}
现在我想做的是查看我的图像(第一个列表)中是否有与第二个列表中的一个匹配的颜色值。如果是这样,那么我想知道第一个列表中的文件名和第二个列表中匹配键的值、id。
我怎样才能做到这一点?
使用 dictionary view objects 在您的 hex
列表和 hex-id 字典之间产生交集:
for entry in images.values():
for key in hexidmap.keys() & entry['hex']:
print('{} {} {}'.format(entry['filename'], key, hexidmap[key]))
&
生成键集和十六进制值列表之间的交集。
以上假设您使用的是Python 3;如果您使用的是 Python 2,请使用 dict.viewkeys()
而不是 .keys()
。
演示:
>>> images = {
... 0: {'hex': ['#c3d6db', '#c7ccc0', '#9a8f6a', '#8a8e3e'], 'filename': 'imag0'},
... 1: {'hex': ['#705b3c', '#6a5639', '#442f1e', '#4a3d28'], 'filename': 'img-xyz'},
... }
>>> hexidmap = {'#b0a7aa': '9976', '#595f5b': '19367', '#9a8f6a': '24095'}
>>> for entry in images.values():
... for key in hexidmap.keys() & entry['hex']:
... print('{} {} {}'.format(entry['filename'], key, hexidmap[key]))
...
imag0 #9a8f6a 24095
for index in d1:
print [(d1[index]["filename"], d2[i], i) for i in d1[index]["hex"] if i in d2]
>>> [('imag0', '24095', '#9a8f6a')]
[]
dicta = {
0: {'hex': ['#c3d6db', '#c7ccc0', '#9a8f6a', '#8a8e3e'], 'filename': 'imag0'},
1: {'hex': ['#705b3c', '#6a5639', '#442f1e', '#4a3d28'], 'filename': 'img-xyz'},
}
dictb = {'#c3d6db': '9976', '#595f5b': '19367', '#9a8f6a': '24095'}
intersection = {}
for o in dicta.values():
intersect = list(set(o['hex']) & set(dictb.keys()))
intersection[o['filename']] = intersect if intersect else "No intersection"
print (intersection)
>>>{'imag0': ['#c3d6db', '#9a8f6a'], 'img-xyz': 'No intersection'}