Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我的目标是从一系列 18090 个列表中生成一个矩阵,每个列表大小为 256。每个列表都被引用为 newmodule.xlist#####。如何循环遍历 newmodule.py 中的变量来创建超级矩阵?
变量的名称可以这样构造:
'xlist%05d' % i
您可以像这样从模块中获取变量:
getattr(newmodule, 'xlist%05d' % i)
要创建“2dim”列表,即列表列表,请执行以下操作:
mat = [ getattr(newmodule, 'xlist%05d' % i) for in range(18090) ]
您可能希望将其转换为numpy2dim 数组:
numpy
mat = numpy.array(mat)
或numpy矩阵:
mat = numpy.mat(mat)