【问题标题】:Access indexed rows in a python list (similar to Matlab cell array)访问 python 列表中的索引行(类似于 Matlab 单元格数组)
【发布时间】:2015-10-05 09:47:38
【问题描述】:

我有一堆不同大小的不同矩阵存储在磁盘上。我需要在 python 中快速处理它们,所以我将每个矩阵加载到内存中并将它们存储在 python 列表中。 我想通过行索引向量选择这些列表条目的子集(相当于在 Matlab 中选择单元格数组中的单元格)在 python 中可能吗?

Matlab 示例如下所示:

allData = cell(100,1); % This cell array contains my different matrices of variable sizes
rowIndices = randi(100,10,1);

selectedData = allData(rowIndices,1);

如何在 python 中做同样的事情?

allData # In python this is a list of "numpy.ndarray"s
rowIndices = random.sample(range(1, numRows), batch_size)
batch_data = allData[rowIndices]

没用

【问题讨论】:

    标签: python


    【解决方案1】:

    一个简单的方法是使用列表推导:

    batch_data = [allData[i] for i in rowIndices]
    

    【讨论】:

      【解决方案2】:

      考虑使用NumPy matrix。看起来它的take() 方法在这种情况下可能会有所帮助。

      【讨论】:

        猜你喜欢
        • 2011-09-23
        • 2018-03-11
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 2019-04-04
        • 2018-08-23
        • 1970-01-01
        • 2018-09-22
        相关资源
        最近更新 更多