python - numpy 数组列表的形状

标签 python arrays list numpy

我在Python中创建了一个由N个numpy数组组成的列表,每个数组的大小为D乘P。当我调用numpy.shape(my_list)时,我返回元组(N, D, P)。当我附加到列表的数组大小不同时(或者如果我附加的项目不是数组),numpy.shape 会抛出错误。

  1. 如果我想要列表中每个数组的形状,我是否必须诉诸列表理解或者是否有更快的方法来做到这一点?
  2. 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/

相关文章:

python - 在 Python 中使用 print 语句时如何删除字符串和参数之间的空格?

c++ - 如何在类中使用数组变量? C++

php - 从数组中删除括号

python - 通过存储具有重复键的多个值,从 2 个列表创建字典

python - 在Python中打印美元符号

python - uwsgi、gevent 和异步循环 - UnboundLocalError : local variable 'query_string' referenced before assignment

C# LINQ 如何将父列表和两个嵌套的子列表展平为一个新列表

python - 给定一组字母,如何创建一个单词与这些字母的所有可能组合,并用指定的数字重复?

Python Pandas : float values break down when df. 列 = df.columns.droplevel()

php - 逗号后加空格