Tarun K Tarun K - 2 months ago 8
Python Question

Replace userinput list elements from master dictionary?

Here, i have a master dic whose value consist of list of list.

Whenever user inputs the value as a list of list, i want my program to replace those user input list from the master list.

I have tried and it is giving me expected output but it consists of lot of temporary lists which is annoying , Is there any other pythonic way to achieve the same without using unwanted temporary list ?

Input dic :

master_dic = {
'First': [[{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}], [{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}],
[{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}], [{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}]],
'Second': [[{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}], [{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}],
[{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}], [{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}]],
'Third': [[{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}], [{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}],
[{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}], [{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}]],
'Forth': [[{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}], [{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}],
[{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}], [{'A': '#d8', 'B': '#d7'}, {'C': '#d8', 'D': '#d7'}]]
}


My Code :

userinput = [[['First'], ['Forth']], [['Second'], ['First']], ['Third'], ['Forth']]

outer = []
for i in userinput:
new = []
if len(i) > 1:
temp = []
for j in i:
jj = master_dic[j[0]]
temp.append(jj)
new.append(temp)
outer.append(new)
else:
for k in i:
kk = master_dic[k]
new.append(kk)
outer.append(new)
data_join = [i[0] for i in outer]


Output :

[
[[[{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}]], [[{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}]]],

[[[{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}]], [[{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}]]],

[[{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}]],

[[{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}], [{'A': '#d8', 'B': '#d7'}, {'D': '#d7', 'C': '#d8'}]]
]


No of list to be maintained

[
[[[ {First value} ]], [[ {Forth value} ]]],

[[[ {Second value} ]],[[ {First value} ]]],

[[ {Third value} ]],

[[ {Forth value} ]]
]

Answer

I think this is what you want

def array_replace(userinput,master_dic):
    for i,data in enumerate(userinput):
        if isinstance(data, list):
            found=array_replace(data,master_dic)
            if not isinstance(found, list):
                userinput[i]=master_dic[found]
        else:
            return data
    return userinput

DEMO

Hope this helps

Comments