I'm python newbie and I'am currently struggling with one problem. I have a list of lists:
[[0,1], [1,2,8], [3,4], [4,2], [2,5], [5,6,7], [8,9,10,11,12]]
My aim is to make a dictionary from it with the following format:
{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]}.
In short:
It has to make a dictionary with unique keys from 1 to 12 in this case (all unique numbers in a list), and to each key it assigns a list of values, which is a common subset for this key i.e for key
2it appears in lists[4,2], [2,5], [1,2,8]so it has to assign to a key2a value which is a list of numbers in those 3 lists except 2.
Thus the output for key 2 should be: 2:[8,1,4,5] etc.
I've made some code:
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) Yet, it gives me output:
{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]}
I really don't know how to combine those lists from this in such a way that I will get desired output.
2: [8,1,4,5]? sublists appear[1,2,8], [3,4], [4,2], [2,5], so it should be2: [1, 8, 4, 5]. Also why1: [2, 8]and not1: [0, 2, 8]?