问题描述
如果我有一个像这样的数组:
If I have an array like:
a = np.array([ [A(2,3 , np.array([[C(2,3)], [C(5,6)] ]))],
[A(4,5 , np.array([[C(1,2)],[C(9,7)]]))]
])
与其他类实例一起,如何访问所有元素?
with other class instances, how can I access all the elements?
例如,
for idx,x in np.ndenumerate(a):
print('Index :{0}'.format(idx))
print('Elmt: {0}'.format(x.the_c[idx].t))
返回:
Index :(0, 0)
Elmt: 2
Index :(1, 0)
Elmt: 9
所以只有2个索引和2个元素,而不是4个.
so only 2 indices and 2 elements instead of 4.
通常,我必须调用另一个ndenumerate
,但是我不确定如何调用它,或者不确定是否有更好(更有效)的方法.
Normally, I have to call another ndenumerate
but I am not sure how to call it or if there is a better (more efficient) way.
代码:
import numpy as np
class A():
def __init__(self, a, b,the_c):
self.a = a
self.b = b
self.the_c = the_c
def methodA(self):
return self.the_c
class B():
def __init__(self, c, d, the_a):
self.c = c
self.d = d
self.the_a = the_a
def evaluate(self):
for idx, x in np.ndenumerate(self.the_a):
x.methodA()
return x.the_c
class C():
def __init__(self,t,y):
self.t = t
self.y = y
如果我想通过调用函数来评估a
数组,该如何调用呢?
And if I want to evaluate the a
array by calling a function, how can I call it?
def evaluate(self):
for idx, x in np.ndenumerate(self.the_a):
x.methodA()
return x.the_c
或
def evaluate(self):
for idx, x in np.ndenumerate(self.the_a):
the_inst = A(x.a, x.b, x.self.the_c)
the_inst.methodA()
return the_inst.the_c
因此,类B中的评价方法将是唯一被调用的方法,它将执行包含许多C实例的许多A实例.
So, the evaluate method in class B will be the only one that gets called and it will execute the many A instances which contain the many C instances.
a = np.array([ [A(2,3 , np.array([[C(2,3)], [C(5,6)] ]))],
[A(4,5 , np.array([[C(1,2)],[C(9,7)]]))]
])
b = B(1,2,a).evaluate()
for idx, x in np.ndenumerate(b):
print(x.t)
给出2 and 5
而不是2,5,1,9
.
推荐答案
a
是2x1数组,其中包含2个对象,均属于类A
:
a
is 2x1 array containing 2 objects, both of class A
:
In [162]: a
Out[162]:
array([[<__main__.A object at 0xab20030c>],
[<__main__.A object at 0xab20034c>]], dtype=object)
我可以使用以下方法将方法调用转换为函数:
I can cast the method call as function with:
def foo(an_A):
return an_A.methodA()
In [164]: a.shape
Out[164]: (2, 1)
In [165]: foo(a[0,0])
Out[165]:
array([[<__main__.C object at 0xab2001cc>],
[<__main__.C object at 0xab2002ec>]], dtype=object)
是另一个2x1数组,这次包含C
个对象.为什么全部都是(2,1)而不是(2,)?
which is another 2x1 array, this time containing C
objects. Is there a particular reason why these are all (2,1) as opposed to (2,)?
frompyfunc
是用于将函数应用于任何数组的所有元素的方便工具,尤其是在输入和输出均为对象数组的情况下:
frompyfunc
is a handy tool for applying a function to all elements of any array, especially when both inputs and outputs are object arrays:
In [166]: f=np.frompyfunc(foo,1,1)
In [167]: f(a)
Out[167]:
array([[ array([[<__main__.C object at 0xab2001cc>],
[<__main__.C object at 0xab2002ec>]], dtype=object)],
[ array([[<__main__.C object at 0xab20096c>],
[<__main__.C object at 0xab2003cc>]], dtype=object)]], dtype=object)
类似于a
,它是(2,1),但现在它包含2(2,1)个C数组.
Like a
, this is (2,1), but now it contains the 2 (2,1) C arrays.
我可以使用它将其转换成C
个对象的(4,1)数组.
I can convert it into a (4,1) array of C
objects with.
In [176]: np.concatenate(f(a)[:,0])
Out[176]:
array([[<__main__.C object at 0xab2001cc>],
[<__main__.C object at 0xab2002ec>],
[<__main__.C object at 0xab20096c>],
[<__main__.C object at 0xab2003cc>]], dtype=object)
np.r_[tuple(f(a)[:,0])]
也会这样做. https://stackoverflow.com/a/42091616/901925
我们可以将连接对象应用于(2,1)f(a)
数组,但是结果更加混乱.
We could apply the concatenate to the (2,1) f(a)
array, but the result is messier.
您还可以使用ndenumerate
来产生与f(a)
相同的东西.首先,您需要创建一个数组,该数组将接收单独的foo(x)
结果:
You could also use ndenumerate
to produce the same thing as f(a)
. First you need to create an array that will receive the individual foo(x)
results:
In [186]: res=np.empty(a.shape, object)
In [187]: for idx,x in np.ndenumerate(a):
...: res[idx] = foo(x)
...:
In [188]: res
Out[188]:
array([[ array([[<__main__.C object at 0xab2001cc>],
[<__main__.C object at 0xab2002ec>]], dtype=object)],
[ array([[<__main__.C object at 0xab20096c>],
[<__main__.C object at 0xab2003cc>]], dtype=object)]], dtype=object)
在1d a
或a[:,0]
上,我们可以使用简单的列表理解:
On a 1d a
or a[:,0]
we can use a simple list comprehension:
In [189]: [foo(x) for x in a[:,0]]
Out[189]:
[array([[<__main__.C object at 0xab2001cc>],
[<__main__.C object at 0xab2002ec>]], dtype=object),
array([[<__main__.C object at 0xab20096c>],
[<__main__.C object at 0xab2003cc>]], dtype=object)]
In [190]: np.array([foo(x) for x in a[:,0]])
Out[190]:
array([[[<__main__.C object at 0xab2001cc>],
[<__main__.C object at 0xab2002ec>]],
[[<__main__.C object at 0xab20096c>],
[<__main__.C object at 0xab2003cc>]]], dtype=object)
In [191]: _.shape
Out[191]: (2, 2, 1)
我很想返回make foo
返回an_A.method()[:,0]
或简化a
:
I'm tempted to go back a make foo
return an_A.method()[:,0]
, or simplify a
:
In [192]: a1 = np.array([ A(2,3 , np.array([C(2,3), C(5,6) ])),
...: A(4,5 , np.array([C(1,2),C(9,7)]))
...: ])
In [195]: np.array([foo(x) for x in a1]) # (2,2) result
Out[195]:
array([[<__main__.C object at 0xab1aefcc>,
<__main__.C object at 0xab1ae94c>],
[<__main__.C object at 0xab1ae0cc>,
<__main__.C object at 0xab1eb2ac>]], dtype=object)
如果我给您的课程repr
方法
def __repr__(self):
return 'A<{0.a},{0.b},{0.the_c}>'.format(self)
def __repr__(self):
return 'C<{0.t},{0.y}>'.format(self)
然后a
显示为
array([[A<2,3,[[C<2,3>]
[C<5,6>]]>],
[A<4,5,[[C<1,2>]
[C<9,7>]]>]], dtype=object)
和f(a)
为
[[array([[C<2,3>],
[C<5,6>]], dtype=object)]
[array([[C<1,2>],
[C<9,7>]], dtype=object)]]
对于B
具有类似的代表,b = B(1,2,a)
显示为
With a similar repr for B
, b = B(1,2,a)
displays as
B<1,2,[[A<2,3,[[C<2,3>]
[C<5,6>]]>]
[A<4,5,[[C<1,2>]
[C<9,7>]]>]]>
和B(1,2,fA(a)).the_a
为(等同于使用f
实现B.evaluate
:
and B(1,2,fA(a)).the_a
as (the equivalent of implementing the B.evaluate
with f
:
[[array([[C<2,3>],
[C<5,6>]], dtype=object)]
[array([[C<1,2>],
[C<9,7>]], dtype=object)]]
这篇关于从数组数组访问元素,调用函数以执行数组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!