Python 如何检测只有一个元素的 numpy 数组的长度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4565749/
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
How detect length of a numpy array with only one element?
提问by mishaF
I am reading in a file using numpy.genfromtxt which brings in columns of both strings and numeric values. One thing I need to do is detect the length of the input. This is all fine provided there are more than one value read into each array.
我正在使用 numpy.genfromtxt 读取文件,该文件包含字符串和数值列。我需要做的一件事是检测输入的长度。只要有多个值读入每个数组,这一切都很好。
But...if there is only one element in the resulting array, the logic fails. I can recreate an example here:
但是...如果结果数组中只有一个元素,则逻辑失败。我可以在这里重新创建一个示例:
import numpy as np
a = np.array(2.3)
len(a) returns an error saying:
len(a) 返回一个错误说:
TypeError: len() of unsized object
however, If a has 2 or more elements, len() behaves as one would expect.
但是,如果 a 有 2 个或更多元素,则 len() 的行为与人们预期的一样。
import numpy as np
a = np.array([2.3,3.6])
len(a) returns 2
len(a) 返回 2
My concern here is, if I use some strange exception handling, I can't distinguish between a being empty and a having length = 1.
我在这里担心的是,如果我使用一些奇怪的异常处理,我无法区分空和长度 = 1。
EDIT: @noskio suggested setting a = np.array([2.3]). The problem is, the actual genesis of a is by using numpy.genfromtxt. The code looks like this:
编辑:@noskio 建议设置 a = np.array([2.3])。问题是, a 的实际起源是使用 numpy.genfromtxt。代码如下所示:
import numpy as np
indata = np.genfromtxt(some_filename, names=True,dtype=None)
a = indata['one_col_headername']
As a result, if indata is only one row in the file, a is a 0-d array.
因此,如果 indata 只是文件中的一行,则 a 是一个 0-d 数组。
采纳答案by pberkes
回答by nosklo
a = np.array([2.3])
print len(a)
回答by unutbu
import numpy as np
tests=[np.array(2.3),np.array([]),np.array([2.3]),np.array([2.3,3.6])]
print('{a:30}{s:<10}{l:<10}{sl:<10}'.format(a='repr',s='shape',sl='len(shape)',l='length'))
for a in tests:
s=a.shape
l=len(a) if a.shape else 0
sl=len(s)
print('{a!r:30}{s:<10}{l:<10}{sl:<10}'.format(a=a,l=l,s=s,sl=sl))
yields
产量
repr shape length len(shape)
array(2.2999999999999998) () 0 0
array([], dtype=float64) (0,) 0 1
array([ 2.3]) (1,) 1 1
array([ 2.3, 3.6]) (2,) 2 1
You can distinguish between an "empty" array (e.g. np.array([])) and a numpy scalar (e.g. np.array(2.3)) by looking at the length of the shape.
您可以通过查看形状的长度来区分“空”数组(例如np.array([]))和 numpy 标量(例如np.array(2.3))。
回答by Brandon
It looks like the sizeproperty of ndarrays will work in this case if you know that the array is one-dimensional. In my opinion, a.sizeis a lot more readable than len(np.atleast_1d(a)). However, note that the sizeproperty will return the totalnumber of elements in the array if it has more than one dimension:
size如果您知道数组是一维的,则看起来ndarrays的属性在这种情况下会起作用。在我看来,a.size它比len(np.atleast_1d(a)). 但是,请注意,如果该size属性具有多个维度,则该属性将返回数组中的元素总数:
In [1]: import numpy as np
In [2]: np.array(2.3).size
Out[2]: 1
In [3]: np.array([1, 2]).size
Out[3]: 2
In [4]: np.array([[1,2], [3,4]]).size
Out[4]: 4

