Python 如何从列表中获取二维列表,但形状在另一个列表中定义

Python 如何从列表中获取二维列表,但形状在另一个列表中定义,python,python-3.x,list,Python,Python 3.x,List,例如,我的列表类似于: a = [143, 146, 152, 235, 246, 468, 476, 607, 615, 707, 712] 我想将其更改为二维列表,格式如下: b = [ [143, 146, 152], [235, 246], [468, 476], [607, 615], [707, 712] ] 或: 但是从带有形状的=[3,2,2,2,2] 如何实现这一目标? 我有这样的想法: devu = '' for i in b:

例如,我的
列表类似于:

a = [143, 146, 152, 235, 246, 468, 476, 607, 615, 707, 712]
我想将其更改为二维
列表
,格式如下:

b = [
    [143, 146, 152],
    [235, 246],
    [468, 476],
    [607, 615],
    [707, 712]
]
或:

但是从带有形状的
=[3,2,2,2,2]

如何实现这一目标? 我有这样的想法:

devu = ''
for i in b:
    for j in range(i):
        devu += str(b[j])

devu += ' '
print(devu)
但结果是:

143146152 143146 143146 143146 143146 

这是一种方法。使用
iter
和一个简单的迭代

演示:

a=[143, 146, 152, 235, 246, 468, 476, 607, 615, 707, 712]
a = iter(a)
shape =[3,2,2,2,2]

result = []
for s in shape:
    temp = []
    for i in range(s):
        temp.append(next(a))
    result.append(temp)
print(result)
[[143, 146, 152], [235, 246], [468, 476], [607, 615], [707, 712]]
输出:

a=[143, 146, 152, 235, 246, 468, 476, 607, 615, 707, 712]
a = iter(a)
shape =[3,2,2,2,2]

result = []
for s in shape:
    temp = []
    for i in range(s):
        temp.append(next(a))
    result.append(temp)
print(result)
[[143, 146, 152], [235, 246], [468, 476], [607, 615], [707, 712]]

实际上,您可以迭代
列表,在迭代时使用索引获取切片,然后创建结果数组

a=[143, 146, 152, 235, 246, 468, 476, 607, 615, 707, 712]

groups = [3,2,2,2,2]

res = []

idx=0
#Iterate over groups, and calculate indexes for slicing
for group in groups:
    res.append(a[idx:idx+group])
    #Increment indexes accordingly
    idx+=group

print(res)
输出是

[[143, 146, 152], [235, 246], [468, 476], [607, 615], [707, 712]]

如果您正在使用数据科学堆栈(例如,
pandas


下面是一个使用
numpy
的解决方案:

import numpy as np
a = [143, 146, 152, 235, 246, 468, 476, 607, 615, 707, 712]
sizes = [3, 2, 2, 2, 2]
assert(sum(sizes) == len(a))
indices = np.cumsum(sizes)[:-1] # Ignore the last size; just take everything that is left (avoids a dangling empty list at the end)
result = [array.tolist() for array in np.split(a, indices)]
# >>> print(result)
# [[143, 146, 152], [235, 246], [468, 476], [607, 615], [707, 712]]
一行程序版本:

result = [array.tolist() for array in np.split(a, np.cumsum(sizes)[:-1])]
您可以使用累积(从itertools)计算要提取的子范围的开始索引和结束索引:

a     = [143, 146, 152, 235, 246, 468, 476, 607, 615, 707, 712]
shape = [3,2,2,2,2]

from itertools import accumulate
b = [ a[s:e] for s,e in zip(accumulate([0]+shape),accumulate(shape)) ]

print(b) # [[143, 146, 152], [235, 246], [468, 476], [607, 615], [707, 712]]
您也可以使用来自functools的reduce来执行此操作(但运行速度会慢得多):


它们是否按
0-99、100-199、200-299
进行分组?您可以将循环内部的行更改为
devu+=''+str(b[j])
?不,分组应按形状进行,如我上面提到的列表中定义:[3,2,2,2,2]嗨@SurajGhale我看到您接受了我的答案,然后又不接受我的答案。如果答案中有不清楚的地方,请告诉我,我可以为您澄清:)不确定我们是否应该在这里使用of迭代器,并且内部for循环是不必要的
a     = [143, 146, 152, 235, 246, 468, 476, 607, 615, 707, 712]
shape = [3,2,2,2,2]

from itertools import accumulate
b = [ a[s:e] for s,e in zip(accumulate([0]+shape),accumulate(shape)) ]

print(b) # [[143, 146, 152], [235, 246], [468, 476], [607, 615], [707, 712]]
from functools import reduce
b = reduce(lambda b,s: b[:-1]+[b[-1][:s],b[-1][s:]], shape, [a])[:len(shape)]