我有一个不同长度的子列表的二维列表,我需要将列表转换为一个 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
我是一名优秀的程序员,十分优秀!