2017-02-22 8 views
0

の1つの配列を返す私はこのコードを持っている:私はstatをするためにstartp関数を呼び出しています収量、ジェネレータオブジェクトを取り除く、代わりに3

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) 

まで、下からプロセス。

ASの関数に渡すために、特定の条件(名前とメソッド)が満たされたときに、valuesリストを初期化して入力しています。

次に、リスト内のすべてのオブジェクトに対してA関数を呼び出すためにyieldを使用します。

A機能は、特定の基準が満たされたときにSまたはV機能をチェックして実行します。私はreturn np.array(thevalues)を使用している場合、私は受け付けており、

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インサイド:

[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] 
] 

:それは可能であれば私が欲しいもの

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

は結果に一つだけの配列を受け取るために、ありますそして、私はstartpに収穫明細を残す必要があります(基本的に1つの利回りは良いです、私はvaluesSに渡したいので、2つの利回りを使用します)

+0

は、ご希望の出力は何ですか? '[0,1,2,3]'や '[3,4,5,6]'? – AsheKetchum

+0

@AsheKetchum:投稿の終わりを確認してください。私は何の出力が必要なのかを書いています.1つの配列を私が言うようにしたいです。ありがとうございます – George

+0

3アレイの配列ですか? – AsheKetchum

答えて

0

最後に、私が使用:

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) 
+0

したがって、 startpで? –

+0

@StephenRauch:これは私が上に来ることができる唯一の解決策です。あなたが別のものを持っているなら、答えをしてください。ありがとう。 – George

関連する問題