如何从列表列表转换为列表元素不均匀的字典?

How to convert from a list of list to dictionary with uneven list elements?

我有以下列表:

list_list = [("111", "222", "455"), ("134", "222", "666"), ("465", "364"), ("324", "364")]

我希望第一个元素是值,其余元素是字典中的键。我还想为一个键设置多个值。这是我想要的结果:

{'222': ['111' '134'], '455': ['111'], '666': ['134'], '364': ['465', '324']} 

我按照下面的方法试过了,还是不行。请帮助我

c = {}
            
for item in list_list:
                
    value = item[0]
                
    for each in range(1, len(item)):
                    
        c[item[each]] = value
            
print(c)

你可以这样实现:

list_list = [("111", "222", "455"), ("134", "222", "666"), ("465", "364"), ("324", "364")]

c = {}

for item in list_list:

    value = item[0]

    for each in range(1, len(item)):
        if item[each] in c.keys():
            c[item[each]].append(value)
        else:
            c[item[each]] = [value]

print(c)

对于每个新值,我们检查键是否已经存在。在您的解决方案中,您覆盖已经具有值的键,这里我们要么创建列表,要么尽可能附加到现有列表。

输出:

{'222': ['111', '134'], '455': ['111'], '666': ['134'], '364': ['465', '324']}