2D列表到numpy数组,并用-1填充较短子列表的剩余值

时间:2013-05-17 02:12:20

标签: python numpy

我有一个不同长度的二维列表,我需要将列表转换为一个numpy数组,以便更短的子列表的所有剩余值都填充-1,我正在寻找一种有效的方法做这个。

例如我有2-D列表x:

x = [
    [0,2,3],
    [],
    [4],
    [5,6]]

我希望得到一个看起来像这样的numpy数组:

>>> array_x
array([[ 0,  2,  3],
       [-1, -1, -1],
       [ 4, -1, -1],
       [ 5,  6, -1]]) 

执行此操作的基本方法是创建一个-1s数组,然后遍历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

但是有更有效的解决方案吗?

1 个答案:

答案 0 :(得分:3)

对原始解决方案进行一些速度改进:

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