展开python字典以表示共享列表值

展开python字典以表示共享列表值,python,dictionary,Python,Dictionary,假设我在Python3.x中有以下简单字典: example = {1:[4, 5, 6], 2:[7, 8, 9]} 我想通过以下方式扩展字典: expanded_example = {1:[4, 5, 6], 2:[7, 8, 9], 4:[5, 6], 5:[4, 6], 6:[4, 5], 7:[8, 9], 8:[7, 9], 9:[7, 8]} 由于多个键共享的值,这变得非常复杂。例如, example2 = {1:[4, 5, 6], 2:[4, 7, 8, 9]} 这里4是

假设我在Python3.x中有以下简单字典:

example = {1:[4, 5, 6], 2:[7, 8, 9]}
我想通过以下方式扩展字典:

expanded_example = {1:[4, 5, 6], 2:[7, 8, 9], 4:[5, 6], 5:[4, 6], 6:[4, 5], 7:[8, 9], 8:[7, 9], 9:[7, 8]}
由于多个键共享的值,这变得非常复杂。例如,

example2 = {1:[4, 5, 6], 2:[4, 7, 8, 9]}
这里4是与1和2关联的列表中的值

如果存在“重复”值元素,则有两种方法:

(1) 仅保留与某个键直接关联的值:

{1:[4, 5, 6], 2:[4, 7, 8, 9], 4:[5, 6], 5:[4, 6], 6:[4, 5], 7:[8, 9], 8:[7, 9], 9:[7, 8]}
(2) 保留所有关联的值(因为“4”在键“1”和“2”之间共享):

编辑:

我的想法是使用集合。defaultdict:

from collections import defaultdict
dict1 = {1:[4, 5, 6], 2:[4, 7, 8, 9]}
d_dict = defaultdict(list)

for k,l in dict1.items():
    for v in l:
        d_dict[v].append(l)

print(d_dict)
## defaultdict(<class 'list'>, {4: [[4, 5, 6], [4, 7, 8, 9]], 5: [[4, 5, 6]], 6: [[4, 5, 6]], 7: [[4, 7, 8, 9]], 8: [[4, 7, 8, 9]], 9: [[4, 7, 8, 9]]})
从集合导入defaultdict
dict1={1:[4,5,6],2:[4,7,8,9]}
d_dict=默认dict(列表)
对于dict1.items()中的k,l:
对于l中的v:
d_dict[v].附加(l)
打印(d_dict)
##defaultdict(,{4:[[4,5,6],[4,7,8,9]],5:[[4,5,6]],6:[[4,5,6]],7:[[4,7,8,9]],8:[[4,7,8,9]],9:[[4,7,8,9]})

这让我明白了一些道理,但是列表列表中有重复的元素…

注意:这个答案只涉及方法1

您可以使用数据的副本,因为在迭代视图时不应添加/删除字典项:

d = {1:[4, 5, 6], 2:[7, 8, 9]}

for k, v in list(d.items()):
    for w in v:
        L = v.copy()
        d[L.pop(L.index(w))] = L

print(d)

{1: [4, 5, 6], 2: [7, 8, 9], 4: [5, 6], 5: [4, 6],
 6: [4, 5], 7: [8, 9], 8: [7, 9], 9: [7, 8]}

战略2

example2 = {1:[4, 5, 6], 2:[4, 7, 8, 9]}
output = {**example2}

for val in example2.values():
    for idx,v in enumerate(val):
        if v not in output:
            output[v] = val[0:idx]+val[idx+1:]
        else:
            output[v].extend(val[0:idx]+val[idx+1:])

print(output)
#{1: [4, 5, 6], 2: [4, 7, 8, 9], 4: [5, 6, 7, 8, 9], 5: [4, 6], 6: [4, 5], 7: [4, 8, 9], 8: [4, 7, 9], 9: [4, 7, 8]}
战略1

import copy
example2 = {1:[4, 5, 6], 2:[4, 7, 8, 9]}
output = copy.deepcopy(example2)

for val in example2.values():
    for num in val:
        if num in output:
            val.remove(num)

    for idx,v in enumerate(val):
        output[v] = val[0:idx]+val[idx+1:]

print(output)
#{1: [4, 5, 6], 2: [4, 7, 8, 9], 4: [5, 6], 5: [4, 6], 6: [4, 5], 7: [8, 9], 8: [7, 9], 9: [7, 8]}

你想使用这两种重复策略中的哪一种?到目前为止你尝试了什么?@BlackThunder抱歉,我没有包括这些来保持问题的清晰性。请参见编辑。这更接近于第二种方法。@OliverRadini我更愿意两者兼而有之actually@jpp对不起,我搞糊涂了:我在哪里改的问题?@ShanZhengYang,哎呀,我错了,你编辑了你的尝试。反正我只处理了一部分。我建议你在第二部分尝试类似的方法。
import copy
example2 = {1:[4, 5, 6], 2:[4, 7, 8, 9]}
output = copy.deepcopy(example2)

for val in example2.values():
    for num in val:
        if num in output:
            val.remove(num)

    for idx,v in enumerate(val):
        output[v] = val[0:idx]+val[idx+1:]

print(output)
#{1: [4, 5, 6], 2: [4, 7, 8, 9], 4: [5, 6], 5: [4, 6], 6: [4, 5], 7: [8, 9], 8: [7, 9], 9: [7, 8]}