yield,去掉生成器对象,return 一个数组而不是三个

yield, get rid of generator object, return one array instead of three

我有这个代码:

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 函数来统计进程。

我在满足某些条件(名称和方法)时初始化并填充 values 列表,以便将其传递给函数 AS.

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

A 函数会在满足特定条件时检查并运行 SV 函数。

使用此数据:

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 函数中,如果我使用 return 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]

如果我使用 yield thevalues,我收到:

<generator object test.V at 0x7f74ec99d678>
<generator object test.S at 0x7f74fc1c0bf8>
[3 4 5 6]
<generator object test.S at 0x7f74ec99d678>
[3 4 5 6]

但是如果我使用:

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 中保留 yield 语句(基本上一个 yield 就可以了,我只使用 2 个 yield 因为我想将 values 传递给 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

我收到了:

temp = []
for i in obs:
    for j in i:
        temp.extend(j)

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

(3,4)