我在Python中创建了一个由N个numpy
数组组成的列表,每个数组的大小为D乘P。当我调用numpy.shape(my_list)
时,我返回元组(N, D, P)
。当我附加到列表的数组大小不同时(或者如果我附加的项目不是数组),numpy.shape
会抛出错误。
- 如果我想要列表中每个数组的形状,我是否必须诉诸列表理解或者是否有更快的方法来做到这一点?
- numpy 是否只是简单地迭代列表,检查以确保每个元素都是与前一个元素大小相同的数组,并根据该结果决定是返回元组还是抛出异常错误?
最佳答案
If I want the shape of each array in the list, do I have to resort to list comprehension or is there a faster way to do this?
列表理解。
Does numpy simply iterate through the list, checking to make sure that each element is an array of the same size as the previous one, and decide based on that whether to return a tuple or throw an error?
NumPy calls asarray
on the list ,构建整个数组只是为了获得形状。 (这不是任何人都费心优化的事情。)
关于python - numpy 数组列表的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43237211/