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

标签 python algorithm python-3.x dictionary merge

如何将具有公共(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/

相关文章:

Python 正则表达式拆分多个空格

python - 从 Python 发送电子邮件

java - 获取 n x n 网格中所有可能的下边缘和右边缘路径

python - Pandas groupby object.aggregate 具有自定义列表操作功能

mysql - 如何修复Failed building wheel for mysqlclient in flask的错误

python - 多次登录才能通过 Azure AD 进行记录

python - Django DLL加载失败: The specified module could not be found

c - 将完整二叉树存储到数组中的算法

algorithm - 每条路径中出现的最少边数

python-3.x - 如何在.kv文件中实现for循环