Python 放弃生成器对象,返回一个数组而不是三个数组

Python 放弃生成器对象,返回一个数组而不是三个数组,python,numpy,yield,Python,Numpy,Yield,我有以下代码: import numpy as np class B(): def __init__(self,a,b): self.a = a self.b = b def __repr__(self): return 'B({0},{1})'.format(self.a,self.b) class test(): def __init__(self,name,method, measures=None):

我有以下代码:

import numpy as np

class B():
    def __init__(self,a,b):
        self.a = a
        self.b = b

    def __repr__(self):
        return 'B({0},{1})'.format(self.a,self.b)

class test():

    def __init__(self,name,method, measures=None):
        self.name = name
        self.method = method
        self.measures = measures

    def __repr__(self):
        return 'test({0.name},{0.method},{1})'.format(self,self.measures)

    def A(self,thevalues,another):

            if self.method == 'S' and another != self.name: 
                yield self.S(thevalues)

            if self.method == 'V':
                yield self.V(thevalues)

    def S(self, thevalues):
        # Change thevalues and return the updated 
        thevalues = np.array(thevalues)
        new_values = thevalues + 3
        yield new_values

    def V(self,thevalues):
        yield thevalues
        #return np.array(thevalues)

class basic():

    def __init__(self, name, thelist):
        self.name = name
        self.thelist = thelist

    def startp(self):
        values = []
        for i in self.thelist:
            if i.name == self.name and i.method != 'S':
                # Just fill some values to server as input to `A`
                for j in range(4):
                    values.append(j)
            # The `A` function will determine which functions inside 
            # `test` it will call
            yield i.A(values,self.name)
从底部开始,向上:

我正在调用
startp
函数来统计进程

当满足某些条件(名称和方法)时,我正在初始化并填写
列表,以便将其传递给函数
A
S

然后,我使用yield开始为列表中的每个对象调用
函数

当满足某些条件时,
A
功能检查并运行
S
V
功能

使用这些数据:

b1 = np.array([B(1,2), B(3,4)])
b2 = np.array([B(5,6), B(7,8)])
b3 = np.array([B(9,10), B(11,12)])

alist = [ test('a','V',b1), test('b','S',b2), test('c','S',b3)]
obs = basic('a',alist).startp()
for i in obs:
   for j in i:
       print(j)
V
函数中,如果我使用
返回np.array(thevalues)
,我将收到:

[0 1 2 3]
<generator object test.S at 0x7f74fc1c0b48>
[3 4 5 6]
<generator object test.S at 0x7f74fc1c0bf8>
[3 4 5 6]
temp = []
for i in obs:
    for j in i:
        temp.extend(j)

temp = np.array(temp)
print(temp.shape)

(3,4)
但如果我使用:

for i in obs:
    for j in i:
        #print(j)
        for k in j:
            print(k)
我可以访问
V
生成的值,但结果是:

[0, 1, 2, 3]
[3 4 5 6]
3
4
5
6
[3 4 5 6]
3
4
5
6
我想要的是,如果可能的话,只接收一个结果为:

[ [0 1 2 3],
  [3 4 5 6],
  [3 4 5 6]
]
我必须在
startp
中保留收益率语句(基本上一个收益率是可以的,我只使用两个收益率,因为我想将
值传递给
S
函数)

最后,我使用了:

def startp(self):
        values = []
        result = []
        for i in self.thelist:
            if i.name == self.name and i.method != 'S':
                # Just fill some values to server as input to `A`
                 for j in range(4):
                    values.append(j)

            values = np.array(values)
            result.extend(i.A(values,self.name))

        result = np.array(result)
        return result
我收到:

[0 1 2 3]
<generator object test.S at 0x7f74fc1c0b48>
[3 4 5 6]
<generator object test.S at 0x7f74fc1c0bf8>
[3 4 5 6]
temp = []
for i in obs:
    for j in i:
        temp.extend(j)

temp = np.array(temp)
print(temp.shape)

(3,4)

你期望的输出是什么<代码>[0,1,2,3]
[3,4,5,6]
?@AsheKetchum:请检查帖子的结尾,我正在写我想要的输出。我想要我说的一个数组。谢谢三个数组的数组?@AsheKetchum:一个3x4数组,正如你所看到的(最好使用
V
中产生值)在
startp()中无法保持产量
并满足您生成单一列表的要求。你可以在
startp()中保持收益率,这样可以吗?那么,我必须在startp中保持收益率的要求发生了什么事?@stephernauch:这是我能想到的唯一解决方案。如果你有另一个,请回答。谢谢。