Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在字典中列出不同列表的第n项_Python_Dictionary_List Comprehension - Fatal编程技术网

Python 在字典中列出不同列表的第n项

Python 在字典中列出不同列表的第n项,python,dictionary,list-comprehension,Python,Dictionary,List Comprehension,我在列表列表中嵌入了字典,例如: X = \ [ [ {'the_geom': (999,999), 1: [111,112,113], 2: [121,122,123]}, {'the_geom': (998,998), 1: [211,212,213], 2:[221,222,223]} ], [ {'the_geom': (997,997), 1: [1111,1112,1113, 1114], 2: [1121,11

我在列表列表中嵌入了字典,例如:

X = \
[
    [
        {'the_geom': (999,999), 1: [111,112,113], 2: [121,122,123]},
        {'the_geom': (998,998), 1: [211,212,213], 2:[221,222,223]}
    ],
    [
        {'the_geom': (997,997), 1: [1111,1112,1113, 1114], 2: [1121,1122,1123, 1124]},
        {'the_geom': (996,996), 1: [1211, 1212, 1213], 2: [2211,2212,2213]}
    ]
]
我正在寻找一个函数,该函数将提供:

XX = \
[
    [
        {'the_geom': (999,999), 'values': [[111, 121], [112,122], [113, 123]]},
        {'the_geom': (998,998), 'values': [[211,221], [212,222], [213,223]]}
    ],
    [
        {'the_geom': (997,997), 'values': [[1111,1121],[1112,1122],[1113,1123],[1114,1124]]},
        {'the_geom': (996,996), 'values': [[1211, 2211], [1212,2212],[1213,2213]]}
    ]
]

我该怎么做?

您可以这样做:

new_x = []
for item in X:
    new_inner_item = []
    for inner_item in item:
        new_inner_item.append({
            'the_geom': inner_item['the_geom'],
            'values': [list(a) for a in zip(*[v for k,v in inner_item.items() if k != 'the_geom'])]
        })
    new_x.append(new_inner_item)

它可以写在一行中,但当格式与原始问题平行时,更容易理解。它处理内部dict中有多个键(例如除1和2以外的键)的情况


您可以将zip与列表理解一起使用:

[ 
    [ 
        { 'the_geom':dict['the_geom'], 
          'values':zip(*[dict[i+1] for i in range(len(dict)-1)])
        } 
        for dict in list
    ] 
    for list in X
] 

难看的变量名,但它可以工作:

XX=[]
for x in X:
    xx=[]
    for i in x:
        ii={}
        ii['the_geom']=i['the_geom']
        ii['values']=[]
        for indx in range(0,len(i[1])):
            pair=[]
            for j in range(1,len(i)):
                pair.append(i[j][indx])
            ii['values'].append(pair)
        xx.append(ii)
    XX.append(xx)
输出:

[[{'values': [[111, 121], [112, 122], [113, 123]], 'the_geom': (999, 999)}, {'values': [[211, 221], [212, 222], [213, 223]], 'the_geom': (998, 998)}], [{'values': [[1111, 1121], [1112, 1122], [1113, 1123]], 'the_geom': (997, 997)}, {'values': [[1211, 2211], [1212, 2212], [1213, 2213]], 'the_geom': (996, 996)}]]

在每本字典中总是只有两个数字列表吗?i、 e.关键1和2?感谢您的关注。不,事实上列表的数量是非常可变的,但我想让示例尽可能简单。你应该从提供的列表中接受一个答案,这样它可以帮助其他人。根据OP的说法,“列表的数量是非常可变的”(例如,多个键,而不仅仅是内_项[1]和内_项[2]),这不会产生预期的输出,您应该
zip
内部列表签名。现已修复。这不会产生预期的输出。OP输出是一个lists@Ohad艾坦:谢谢你的留言,科雷克德
res1= [[{'the_geom':y['the_geom'],'values':[[y[1][i],y[2][i]] for i in range(3)]} for y in z] for z in X]
[[{'values': [[111, 121], [112, 122], [113, 123]], 'the_geom': (999, 999)}, {'values': [[211, 221], [212, 222], [213, 223]], 'the_geom': (998, 998)}], [{'values': [[1111, 1121], [1112, 1122], [1113, 1123]], 'the_geom': (997, 997)}, {'values': [[1211, 2211], [1212, 2212], [1213, 2213]], 'the_geom': (996, 996)}]]