gpt4 book ai didi

将键与单个字典的公共(public)值合并的 Pythonic 方法

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:27:36 24 4
gpt4 key购买 nike

如何将具有公共(public)值的字典的键合并到一个元组中。例如:

A = {'E2': {'5', '7'}, 'E3': {'4', '8'}, 'E5': {'5', '7'}, 'E8': {'4', '8'}}

output = {('E2', 'E5'): {'5', '7'}, ('E3', 'E8'): {'4', '8'}}

我的尝试:

A = {'E2': {'5', '7'}, 'E3': {'4', '8'}, 'E5': {'5', '7'}, 'E8': {'4', '8'}}

output = {}
seen = []
for k, v in A.items():
if v not in [s[1] for s in seen]: # not seen this value yet
print('NOT SEEN')
print(k, v)
seen.append([k,v])
output[k] = v
else: # already seen it
print('SEEN')
print(k, v)
# determine where we've seen it
where = [x for x in seen if x[1]==v]
output.pop(where[0][0])
output[(where[0][0], k)] = v


print('OUTPUT = ', output)

这打印:

OUTPUT =  {('E2', 'E5'): {'7', '5'}, ('E3', 'E8'): {'4', '8'}}

最佳答案

我会分两次进行转换:

>>> A = {'E2': {'5', '7'}, 'E3': {'4', '8'}, 'E5': {'5', '7'}, 'E8': {'4', '8'}}

# First pass: Create a reverse one-to-many mapping.
# The original set() value gets converted to a hashable frozenset()
# and used as a key. The original scalar string key gets accumulated
# in a list to track the multiple occurrences.
>>> reverse = {}
>>> for key, value in A.items():
reverse.setdefault(frozenset(value), []).append(key)

# Second pass: reverse the keys and values. The list of matching
# values gets converted to a hashable tuple (as specified by the OP)
# and the frozenset() gets restored back to the original set() type.
>>> {tuple(value) : set(key) for key, value in reverse.items()}
{('E2', 'E5'): {'5', '7'}, ('E3', 'E8'): {'8', '4'}}

这给出了 OP 预期的输出。

请注意,输入字典没有保证顺序,原始输入中的任何集合也没有。因此,输出不能保证术语的顺序。

关于将键与单个字典的公共(public)值合并的 Pythonic 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44991051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com