Click here to Skip to main content
15,667,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have 2 dictionaries and one of them is nested. I would like to create another nested dictionary by checking keys and values of these two dictionaries. 'dict1' gives the connections between the key and the elements of the list:
dict1={'A': ['K', 'J'], 'C': ['A'], 'D': ['B', 'C']}

dict2 has same keys (less or more) compared to dict1 as:
dict2={'D': {'D': '0.20','B': '0.20','C': '0.00','A': '0.06','K': '0.00','J': '0.02'},'A':{'K':'3','J':'3'}}

For a given key in the first level and all the other keys in the second level of dict2, I would like to create a nested dictionary which checks dict1 and its list to see the connections. If an element from the list of dict1 (which matches with second level of the nested dict2) has zero value in dict2, it should checks its list connection in dict1, and do the connection with dict1 elements until it reaches to non-zero value. For instance, 'D' in dict1 has list of ['B','C']. 'B':0.20 which is non-zero in dict2 so we can do connection as ('D','B',[]). Then, we check for 'D' and 'C'. Given 'D' and 'C':'0.00', we check dict1 for 'C' and see that it has list of ['A'] which is non-zero in dict2 ('A': '0.06') so we get ('D','A',[C]). If 'A' was zero in dict2 then we should have checked dict1 to see that 'A': [['K', 'J'] then check whether 'K' and 'J' are non-zero. If they were non-zero then we should have gotten (D,K,[]) and (D,J,[]). Given these, I would like to obtain a nested dictionaries like below so that I can have the connections and list of keys which are like intermediary within my connections:

result={'D': {('D','B',[]),('D','A',['C']),('A','J',[])}'A':{('A','K',[]),('A','J',[])}}

'result' should give me the connections and also the list that shows if I have had any element between the connected ones while creating the connections. This is how far I could get but I cant get my intended output whereas I have bigger data unlike the example here:

What I have tried:

output = []
for target in dict2:
    for key in dict1:
        for i in dict1[key]:
            if dict2[target][i]!='0.00':
                output.append((key, i))

Updated 31-Aug-22 4:19am
Richard MacCutchan 31-Aug-22 6:46am    
You can only use recursion by writing a function that calls itself. But you really need to explain what the recursion will actually do.
Member 15753358 31-Aug-22 6:52am    
Actually, using recursion was my guess in order to solve this problem since I dont know how to achieve the result1 or result2.

1 solution

Recursion is a poor idea, particularly if you have a large set of data to process: each recursive call uses more stack space to store the return address and the local variables, and stacks are generally pretty small - only around 1MB.
So if your data set gets large, you will very quickly exhaust all the available stack space, and your app with crash.
Only use recursive methods when processing genuinely recursive data and when the sample size is guaranteed to be pretty small.

I'd start by doing the job manually on paper to get a good idea what you need to do to generate your outputs from sample inputs. When you have that sorted out, computerizing it should be relatively simple.

This may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Share this answer
Member 15753358 31-Aug-22 7:28am    
Using recursion was just an idea but as in the trial code I have tried another way of doing it and you could see from my question, I have used simple example and the output that I want to obtain. I However, I couldnt manage to get the result. Thanks tho

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900