从master字典中替换userinput list元素?

时间:2016-09-07 07:29:01

标签: python list dictionary

这里,我有一个主dic,其值由列表列表组成。

每当用户将值作为列表列表输入时,我希望我的程序从主列表中替换这些用户输入列表。

我已经尝试了,它给了我预期的输出,但它包含很多临时列表,这很烦人,有没有其他pythonic方法来实现相同而不使用不需要的临时列表?

输入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'}]]
}

我的代码:

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]

输出:

[
    [[[{'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'}]]
]

要保留的清单

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

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

    [[ {Third value} ]],

    [[ {Forth value} ]]
]

1 个答案:

答案 0 :(得分:1)

我认为这就是你想要的

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

希望这有帮助