1

I have a dictionary with lists as values and strings as keys. The values are build like this (4, 7) and at least one of both numbers will be in the values from the following key.

The dictionary looks like this:

dot_z = 
{
'1': [[4, 7], [7, 8], [7, 9], [6, 7]],
'2': [[4, 5], [8, 10], [3, 4]],
'3': [[1, 3], [2, 3], [3, 11]],
'4': [[11, 12], [11, 13], [2, 15]]
'5': [[15, 16], [12, 14]]
}

My goal is to receive a new dictionary that should look like this:

7: [4, 8, 9, 6],
4: [5, 3],
8: [10],
9: [],
6: [],
10: [],
5: [],
3: [1, 2, 11],
1: [],
2: [15],
11: [12, 13],
15: [16],
12: [14],
13: []

That means that dict first line number 7 gets removed because it is connected to all of them. Next step is to take all numbers that are connected to 7 and to check to which number they are connected. In the end each number represents a dot that is connected to another number/dot. In the new dictionary the keys represent the starting numbers and their values will contain all numbers that they are connected to.

How is it possible to achieve this?

  • 1
    What have you tried? How does it fail to meet your expectation? – Dennis Williamson Sep 27 '22 at 13:01
  • Looks like the keys `'1', '2', '3', '4', '5'` from the first dictionary are completely irrelevant? – Stef Sep 27 '22 at 13:41
  • 1
    This looks like a [breadth-first search](https://en.wikipedia.org/wiki/Breadth-first_search) in a graph. Have you heard this term before? – Stef Sep 27 '22 at 13:43
  • 1
    Why does 3, for example, appear in the goal dictionary both as a key and in the values, but 7 is only a key and 6 is only in the values? – Dennis Williamson Sep 27 '22 at 13:52
  • This is the result of a bfs actually. ` '1', '2', '3', '4', '5' ` from the first dictionary represent steps. 7 is the source for the search algorithm and after 2 steps the nodes 3, 5 and 10 are reached. This is also why 7 is only a key because it is where it is starting and 3 is key and value because first it gets reached and from 3 on others are also reached. And 6 is no key because I forgot to write it down, I will edit that. Thanks for noticing. – maxwell_742 Sep 27 '22 at 14:20
  • Show the code that you have that attempts to get the result you want and point out the difference between the result you get and the result you want. There's not enough information here to provide any more than guesses. – Dennis Williamson Sep 27 '22 at 15:57
  • 1
    How are 2 and 15 related? – Dennis Williamson Sep 27 '22 at 15:58
  • See [this](https://stackoverflow.com/questions/48333927/python-convert-a-depth-first-search-to-a-breadth-first-search-for-all-combinati). – Dennis Williamson Sep 27 '22 at 18:58
  • I've updated my problem and attempts of solving it in a new question [https://stackoverflow.com/questions/73895528/comparing-two-dictionaries-receiving-a-new-dict-that-contains-shared-informatio] – maxwell_742 Sep 29 '22 at 12:30
  • I’m voting to close this question because the OP has posted a replacement question. – Dennis Williamson Sep 29 '22 at 22:20

0 Answers0