Get Sequences from nested dictionaries

时间:2017-07-12 08:06:54

标签: python python-3.x dictionary nested sequences

Imagine two dictionaries:

dict1 = {0: {'In': [0], 'Out': [1, 2]}, 1: {'In': [1], 'Out': [3, 4]}, 2: {'In': [2], 'Out': [7]}, 3: {'In': [4, 7], 'Out': [10]}, 4: {'In': [3, 10], 'Out': [6]}, 5: {'In': [8], 'Out': [9]}, 6: {'In': [5, 10], 'Out': [11]},7: {'In': [9, 11, 12], 'Out': []}}

dict2 = {0: {'In': [3], 'Out': [5, 6]}, 1: {'In': [6], 'Out': [8, 12]}, 2: {'In': [10], 'Out': [6, 11]}}

, where keys represent nodes and values represent incoming and outgoing operations.

Now I want to get all possible sequences by choosing one outgoing operation, if incoming operation leads to a node in dict1. But I have to choose both operations, if the incoming operation leads to a node in dict2.

For the given example i want to get:

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

I built an endless code with several for/if - Statements, which works for the shown example. But there are still missing values, when I test other instances.

Any hints on restructuring the dictionaries or deriving the sequences would be great.

0 个答案:

没有答案
相关问题