使用唯一键和所有相关值将列表列表转换为字典

时间:2019-05-31 20:14:43

标签: python list dictionary

我是python新手,目前正在努力解决一个问题。我有一个列表列表:

[[0,1], [1,2,8], [3,4], [4,2], [2,5], [5,6,7], [8,9,10,11,12]]

我的目标是使用以下格式从中制作字典:

{0:[1],1:[2,8],2:[8,1,4,5],3:[4],5:[6,7,2],6:[5,7],7:[5,6],8:[1,2,10,9,11,12],9:[8,10,11,12],10:[8,9,11,12],11:[12,10,9,8],12:[11,9,10,8]}

简而言之:

  

在这种情况下,它必须制作一个具有1到12个唯一键的字典(列表中所有唯一的数字),并且它为每个键分配一个值列表,这是此键(例如,键)的常见子集2它出现在列表[4,2], [2,5], [1,2,8]中,因此必须为键2分配一个值,该值是这3个列表中除2之外的数字的列表。

因此,键2的输出应为:2:[8,1,4,5]等。

我已经编写了一些代码:

data = [[0,1], [1,2,8], [3,4], [4,2], [2,5], [5,6,7], [8,9,10,11,12]]

graph = {}
for i in range(13):
    graph[i] = 3

numbers = list(range(0,13))
for row in data:
    for i in graph.keys():
        if i in row:
            graph[i] = row

for key,value in graph.items():
    graph[key] = graph[key][1:]

print(graph)

但是,它给了我输出:

{0: [1], 1: [2, 8], 2: [5], 3: [4], 4: [2], 5: [6, 7], 6: [6, 7], 7: [6, 7], 8: [9, 10, 11, 12], 9: [9, 10, 11, 12], 10: [9, 10, 11, 12], 11: [9, 10, 11, 12], 12: [9, 10, 11, 12]}

我真的不知道如何以这种方式组合这些列表,以便获得所需的输出。

5 个答案:

答案 0 :(得分:2)

d=dict()
entries=[]
for i in range(0,13):
    entries=[]
    for j in data:
        if i in j:
            entries+=[num for num in j if num!=i]
    d[i]=entries

输出

{0: [1],
 1: [0, 2, 8],
 2: [1, 8, 4, 5],
 3: [4],
 4: [3, 2],
 5: [2, 6, 7],
 6: [5, 7],
 7: [5, 6],
 8: [1, 2, 9, 10, 11, 12],
 9: [8, 10, 11, 12],
 10: [8, 9, 11, 12],
 11: [8, 9, 10, 12],
 12: [8, 9, 10, 11]}

答案 1 :(得分:2)

您可以在此处使用defaultdict

from collections import defaultdict

mylist = [[0,1], [1,2,8], [3,4], [4,2], [2,5], [5,6,7], [8,9,10,11,12]]

d = defaultdict(list) # a dictionary with all values defaulting to lists

for lst in mylist:
    for val in lst:
        # extend each list with new values provided they aren't
        # equal to the key in question
        d[val].extend([x for x in lst if x!=val])

d
# {0: [1], 1: [0, 2, 8], 2: [1, 8, 4, 5], 8: [1, 2, 9, 10, 11, 12], 3: [4], 4: [3, 2], 5: [2, 6, 7], 6: [5, 7], 7: [5, 6], 9: [8, 10, 11, 12], 10: [8, 9, 11, 12], 11: [8, 9, 10, 12], 12: [8, 9, 10, 11]}

通过使用defaultdict,您不必担心默认情况下会为值分配列表,因此您可以使用标准的list属性,例如appendextend

出于性能考虑,在lst上进行N + 1次迭代时,其中N是lst的长度。 extend是O(N)操作,因此构建列表扩展现有列表并不能完全有效。它也不会考虑重复的值(如果这对您很重要)。但是,这比尝试使用标准的dict更为干净,您可以这样做:

d = {}
for lst in mylist:
    for val in lst:
        if val not in d:
            d[val] = [x for x in lst if x!=val]
        else:
            d[val].extend([x for x in lst if x!=val])

d
# {0: [1], 1: [0, 2, 8], 2: [1, 8, 4, 5], 8: [1, 2, 9, 10, 11, 12], 3: [4], 4: [3, 2], 5: [2, 6, 7], 6: [5, 7], 7: [5, 6], 9: [8, 10, 11, 12], 10: [8, 9, 11, 12], 11: [8, 9, 10, 12], 12: [8, 9, 10, 11]}

必须检查val是否在字典中以构建list,否则,您可以使用extend就地修改值

答案 2 :(得分:2)

您仅将graph[i]分配给一行,因此,如果遇到包含i的另一行,则将重新分配初始行的答案。相反,您可能想为graph[i]分配一个空列表,而不是为graph[i]附加3个列表。

graph = {}
for i in range(13):
  graph[i] = []

for row in data:
  for i in graph.keys():
    if i in row:
      for val in row:
        if val != i:
          graph[i].append(val)

答案 3 :(得分:1)

您是要专门修复代码还是在寻找解决方案?

data = [[0,1], [1,2,8], [3,4], [4,2], [2,5], [5,6,7], [8,9,10,11,12]]
n = 12

# Create dict with empty list for each key
result = {key: [] for key in range(n + 1)}

for key in result:
    for x in data:
        if key in x:
            # We need to make a copy of `x` because otherwise we'd modify `data`
            x = x.copy()
            x.remove(key)
            result[key].extend(x)

print(result)

这是一种强调清晰度的方法,因此我敢肯定runtime仍需改进。它打印:

{0: [1], 1: [0, 2, 8], 2: [1, 8, 4, 5], 3: [4], 4: [3, 2], 5: [2, 6, 7], 6: [5, 7], 7: [5, 6], 8: [1, 2, 9, 10, 11, 12], 9: [8, 10, 11, 12], 10: [8, 9, 11, 12], 11: [8, 9, 10, 12], 12: [8, 9, 10, 11]}

另外,顺序重要吗?

答案 4 :(得分:1)

.styleGrid{
    border: 1px #BDBDBD solid;
    background-color: white;
    box-shadow: 1px 1px 5px #BDBDBD; 
}
.listOrgs{
    max-height: 800px;
    margin-bottom: 4%;
}
a {
    text-decoration: none;
    color: black;
}

.my-tree-invisible {
  display: none;
}

.my-tree ul,
.my-tree li {
  margin-top: 0;
  margin-bottom: 0;
  list-style-type: none;
}

输出

a=[[0,1], [1,2,8], [3,4], [4,2], [2,5], [5,6,7], [8,9,10,11,12]]
from collections import defaultdict


dic=defaultdict(list)
for i,v in enumerate(a):
    for j, v2 in enumerate(v):        
        new_s=a[i][:j]+a[i][j+1:]
        dic[v2].extend(new_s)

for i in dic:
    dic[i]=list(set(dic[i]))

print(dict(dic))
相关问题