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

标签 python numpy

我有一个不同长度的子列表的二维列表,我需要将该列表转换为 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/

相关文章:

python - Redis 与 Disk 在缓存应用程序中的性能

python - 有效地更新计时器并在 python 中满足检查条件

python - 如何从图像中删除或清除轮廓?

python - 如何从执行进程任务运行 py 文件?

python - python 检查一个数组是否是另一个数组的元素

python - 从外部网站获取元描述

python - 在不可预测的背景下检测叶子

python - 使用日期数据进行 Sklearn 线性回归

python - 如何在 python 中使用一维数组生成旋转对称函数?

Python 翻译 C saxpy