2015-10-05 97 views
0

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

Matlab的例子是这样的:

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

selectedData = allData(rowIndices,1); 

我如何可以做同样的蟒蛇?

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

不起作用

回答

1

一个简单的方法是使用列表理解:

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