查找多维 Python 数组的维度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17531796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:26:48  来源:igfitidea点击:

Find the dimensions of a multidimensional Python array

pythonarraysdimensions

提问by Anderson Green

In Python, is it possible to write a function that returns the dimensions of a multidimensional array (given the assumption that the array's dimensions are not jagged)?

在 Python 中,是否可以编写一个函数来返回多维数组的维度(假设数组的维度不是锯齿状的)?

For example, the dimensions of [[2,3], [4,2], [3,2]]would be [3, 2], while the dimensions of [[[3,2], [4,5]],[[3,4],[2,3]]]would be [2,2,2].

例如,的尺寸[[2,3], [4,2], [3,2]]将是[3, 2],同时的尺寸[[[3,2], [4,5]],[[3,4],[2,3]]]将是[2,2,2]

Does Python have any built-in functions that will return all of the dimensions of a multidimensional array, or will I need to implement this function myself?

Python 是否有任何内置函数可以返回多维数组的所有维度,还是我需要自己实现这个函数?

采纳答案by jason

No, there's nothing built-in because with such "arrays"1it can be jagged and the concept of "dimensions" or "shape" doesn't make any sense at all. So, you'll have to write your own. If you can make an assumption of uniformity along all dimensions, you can proceed as follows:

不,没有任何内置内容,因为使用这样的“数组” 1它可以是锯齿状的,并且“尺寸”或“形状”的概念根本没有任何意义。所以,你必须自己写。如果您可以假设所有维度的均匀性,您可以按照以下步骤进行:

dim1 = len(a)
dim2 = len(a[0])
dim3 = len(a[0][0])
.
.
.

It'd be pretty easy to make this recursive to handle all dimensions. This should do it:

使这个递归处理所有维度非常容易。这应该这样做:

def dim(a):
    if not type(a) == list:
        return []
    return [len(a)] + dim(a[0])

But if you need something like this, you might want to consider looking at NumPyarrays which have numpy.ndarray.shapewhich would give you what you're looking for.

但是,如果您需要这样的东西,您可能需要考虑查看NumPy数组,numpy.ndarray.shape这些数组可以为您提供所需的东西。

from numpy import array
l = [[2, 3], [4, 2], [3, 2]]
a = array(l)
print a.shape

Output

输出

(3, 2)

1In scare quotes because you're not really looking at array, you're looking at a list, or a list of lists, or a list of list of lists....

1在恐吓引号中,因为您并不是真正在查看数组,而是在查看列表、列表列表或列表列表...

回答by David Heffernan

That is not a multi-dimensional array. It is a list. It happens to contain other lists. There's nothing to say that your list could not be:

那不是多维数组。它是一个list. 它恰好包含其他列表。没有什么可以说您的列表不能是:

[[2,3], [4,2], [3,2,4,5,6]]

In which case, what value would you expect such a function to return?

在这种情况下,您希望这样的函数返回什么值?

There is no general function that does what you ask, not least because Python itself does not define a matrix/array class. You certainly can write your own function which operates on iterable objects like lists and tuples if you are prepared to make assumptions, or write assertions, as to the uniformity of the list. Use len(a)for the first dimension, len(a[0])for the second, and so on. Recursion will be your friend here.

没有通用函数可以满足您的要求,尤其是因为 Python 本身没有定义矩阵/数组类。如果您准备对列表的一致性进行假设或编写断言,您当然可以编写自己的函数,该函数对列表和元组等可迭代对象进行操作。使用len(a)第一个维度,len(a[0])第二,等等。递归将在这里成为你的朋友。

If you used a numpy array for your matrix, which to be honest would make a lot of sense, then your function would exist (it is the shapeproperty of the ndarray class) and be meaningful.

如果你为你的矩阵使用了一个 numpy 数组,老实说这很有意义,那么你的函数就会存在(它是shapendarray 类的属性)并且是有意义的。

回答by Ben

You can do it with numpy:

你可以用 numpy 做到这一点:

import numpy
l = [[2,3], [4,2], [3,2]]
m = numpy.array(l)
print m.shape

But the shape of your second example is [2,2,2], not [1,4,5], unless I've misunderstood your question...

但是你的第二个例子的形状是 [2,2,2],而不是 [1,4,5],除非我误解了你的问题......

回答by Archit Jain

Assuming the input array is not jagged :

假设输入数组不是锯齿状的:

def arr_dimen(a):
  return [len(a)]+arr_dimen(a[0]) if(type(a) == list) else []