我有一个不同长度的子列表的二维列表,我需要将该列表转换为 numpy 数组,以便较短子列表的所有剩余值都填充为 -1,并且我正在寻找一种有效的方法做这个。
例如,我有二维列表 x:
x = [
[0,2,3],
[],
[4],
[5,6]]
我想要一个像这样的 numpy 数组:
>>> array_x
array([[ 0, 2, 3],
[-1, -1, -1],
[ 4, -1, -1],
[ 5, 6, -1]])
基本方法是创建一个 -1 数组,然后循环遍历 2D 列表以填充剩余的值,如下所示:
n_rows = len(x)
n_cols = max(len(ele) for ele in x)
new_array = np.ones((n_rows, n_cols)) * -1
for i, row in enumerate(x):
for j, ele in enumerate(row):
new_array[i, j] = ele
但是有更有效的解决方案吗?
最佳答案
对原始解决方案的一些速度改进:
n_rows = len(x)
n_cols = max(map(len, x))
new_array = np.empty((n_rows, n_cols))
new_array.fill(-1)
for i, row in enumerate(x):
for j, ele in enumerate(row):
new_array[i, j] = ele
时间安排:
import numpy as np
from timeit import timeit
from itertools import izip_longest
def f1(x, enumerate=enumerate, max=max, len=len):
n_rows = len(x)
n_cols = max(len(ele) for ele in x)
new_array = np.ones((n_rows, n_cols)) * -1
for i, row in enumerate(x):
for j, ele in enumerate(row):
new_array[i, j] = ele
return new_array
def f2(x, enumerate=enumerate, max=max, len=len, map=map):
n_rows = len(x)
n_cols = max(map(len, x))
new_array = np.empty((n_rows, n_cols))
new_array.fill(-1)
for i, row in enumerate(x):
for j, ele in enumerate(row):
new_array[i, j] = ele
return new_array
setup = '''x = [[0,2,3],
[],
[4],
[5,6]]
from __main__ import f1, f2'''
print timeit(stmt='f1(x)', setup=setup, number=100000)
print timeit(stmt='f2(x)', setup=setup, number=100000)
>>>
2.01299285889
0.966173887253
关于python - 2D 列表到 numpy 数组,并用 -1 填充较短子列表的剩余值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16600397/