字典(dict )动态构造索引,交换位置或者取值;使用eval

if __name__ == "__main__":
    t_struct = [
        {"pid": "", "type": "group", "uuid": "KWQwhvwkO03", "subset": [
            {"pid": "KWQwhvwkO03", "type": "scene", "uuid": "HSBvLCBnSDZ",
             "subset": [{"pid": "HSBvLCBnSDZ", "type": "navigate", "uuid": 3, "subset": []},
                        {"pid": "HSBvLCBnSDZ", "type": "navigate", "uuid": 9, "subset": []}]},
            {"pid": "KWQwhvwkO03", "type": "scene", "uuid": "DDMTyVQCFGV", "subset": []}]},
        {"pid": "", "type": "group", "uuid": "8WeptwyTbua",
         "subset": [{"pid": "8WeptwyTbua", "type": "scene", "uuid": "JtXJTxw2HEz", "subset": []},
                    {"pid": "8WeptwyTbua", "type": "scene", "uuid": "AdaJpYIbKQu", "subset": []},
                    {"pid": "8WeptwyTbua", "type": "scene", "uuid": "DjpxaJBQMSx", "subset": []},
                    {"pid": "8WeptwyTbua", "type": "scene", "uuid": "CqG1RCWWvnh", "subset": []},
                    {"pid": "8WeptwyTbua", "type": "scene", "uuid": "JjzHZ2Co4kp", "subset": []},
                    {"pid": "8WeptwyTbua", "type": "scene", "uuid": "jj50AbA5bO", "subset": []},
                    {"pid": "8WeptwyTbua", "type": "scene", "uuid": "GUjDKNqBvxN", "subset": []}]}]
    nbl = NavigateBarLib().subset_len(t_struct, "8WeptwyTbua")
    print(nbl)
    index = [0, 0, 1]
    print(exec(f"t_struct[{0}]['subset'],t_struct[{1}]['subset'] = t_struct[{1}]['subset'],t_struct[{0}]['subset']"))
    print(t_struct)

注意exec可以执行赋值操作,eval不可

你可能感兴趣的:(Python,python)