루프

2012-10-01 5 views
2

나는 구조의이 종류를했습니다 :루프

[ 
    array([ 0. , 4.5, 9. ]), 
    [ 
     array([ 100., 120., 140.]), 
     [ 
      array([ 1000., 1100., 1200.]), 
      array([ 1200., 1300., 1400.]) 
     ], 
     array([ 150., 170., 190.]), 
     [ 
      array([ 1500., 1600., 1700.]), 
      array([ 1700., 1800.]) 
     ] 
    ] 
] 

(arraynumpy.array들있는 곳) 나에게주는 발전기 작성하는 방법

:

(0, 4.5), (100, 120), (1000, 1100) 
(0, 4.5), (100, 120), (1100, 1200) 
(0, 4.5), (120, 140), (1200, 1300) 
(0, 4.5), (120, 140), (1300, 1400) 
(4.5, 9), (150, 170), (1500, 1600) 
(4.5, 9), (150, 170), (1600, 1700) 
(4.5, 9), (170, 190), (1700, 1800) 

지금까지 내가 가진 유일한 문제는 다음과 같습니다.

def loop_bin(bins): 
    for i in range(len(bins)-1): 
     yield [bins[i], bins[i+1]] 
+0

* array * 란 무엇입니까? 3 호선과 4 호선이 같은 이유는 무엇입니까? – hochl

+0

'array'는'np.array'입니다 –

+0

itertools에 대해 알고 계십니까? http://docs.python.org/library/itertools.html –

답변

1

무엇에 대해 :

def foo(m): 

    for i in range(0, len(m), 2): 

    for j in range(len(m[i])-1): 
     current = tuple(m[i][j:(j+2)]) 
     mm = m[i+1] 
     if(len(mm) % 2 != 0 or (len(mm) > 1 and not type(mm[1][0]) is types.ListType)): 
     currentl = mm[j] 
     for k in range(0, len(currentl)-1): 
      yield current, tuple(currentl[k:(k+2)]) 

     else: 
     for res in foo(mm[2*j:2*j+2]): 
      # this is for pretty print only 
      if type(res) is types.TupleType and len(res)>1 and not type(res[0]) is types.TupleType: 
      yield current, res 
      else: 
      # pretty print again 
      c = [current] 
      c+= res 
      yield tuple(c) 

tuple 일이 당신의 예에 가까이하기 위해, 고급 인쇄를위한 것입니다. 잎 탐지에 대한 기준에 대해서는 잘 모르겠습니다. 나는 다음과 같은 파이썬 배열 내 실험을 만든 것도 참고 :

arr = [ 
    [ 0. , 4.5, 9. ], 
    [ 
     [100., 120., 140.], 
     [ 
      [ 1000., 1100., 1200.], 
      [ 1200., 1300., 1400.] 
     ], 
     [ 150., 170., 190.], 
     [ 
      [ 1500., 1600., 1700.], 
      [ 1700., 1800.] 
     ] 
    ] 
] 

보다는 주어진 NumPy와 배열,하지만 변화를 numarray 실행 일들이 간단해야 얻을.

0

사용 재귀 그것을 할 수 있습니다 :

def foo(lst, path): 
    if type(lst[0]) != type(array([])): 
     return [path+[(lst[0],lst[1])], path+[(lst[1],lst[2])]] 

    i = 0 
    ret = [] 
    while i < len(lst): 
     node = lst[i] 
     successor = lst[i+1] if i+1<len(lst) else None 
     if type(node) == type(array([])): 
      if type(successor) == list: 
       children = successor 
       ret.extend(foo(children, path + [(node[0], node[1])])) 
       ret.extend(foo(children, path + [(node[1], node[2])])) 
       i+=1 
      else: 
       ret.append(path + [(node[0], node[1])]) 
       ret.append(path + [(node[1], node[2])]) 
     i+=1 
    return ret 

전화 foo(input, [])을 계산합니다.

+0

이것은 단지 8 대신 32 개의 항목을 생성합니다. –

+0

@tobias_k, 예,하지만 샘플 목록이 완성되었는지 궁금합니다. .. – Marcus

+0

출력이 잘못되었습니다. –

2

상황을 살펴보면 다음과 같은 몇 가지 유형의 반복으로 나뉩니다. overlappaired (일반 반복은 물론).

그런 다음 유형을 분석하여 표시되는 데이터를 반복하는 방식을 결정하는 dopair의 트리 구조를 반복적으로 탐색합니다. 결정은 노드 (서브 트리를 포함) 또는 리프 (어레이)를 처리 중인지 여부에 따라 결정됩니다.

generatorizip을 사용하면 동시에 두 개의 발전기를 반복 할 수 있습니다.

from itertools import izip 

class array(list): 
    pass 

arr = [ 
    array([ 0. , 4.5, 9. ]), 
    [ 
     array([100., 120., 140.]), 
     [ 
      array([ 1000., 1100., 1200.]), 
      array([ 1200., 1300., 1400.]) 
     ], 
     array([ 150., 170., 190.]), 
     [ 
      array([ 1500., 1600., 1700.]), 
      array([ 1700., 1800.]) 
     ] 
    ] 
] 

# overlap(structure) -> [st, tr, ru, uc, ct, tu, ur, re] 
def overlap(structure): 
    for i in range(len(structure)-1): 
     yield (structure[i],structure[i+1]) 

# paired(structure) -> [st, ru, ct, ur] 
def paired(structure): 
    for i in range(0,len(structure)-1,2): 
     yield (structure[i],structure[i+1]) 

def dopair(first,second): 
    if all(isinstance(x,array) for x in second): 
     for pa,ir in izip(overlap(first),second): 
      for item in overlap(ir): 
       yield pa, item 
    else: 
     for pa,(i,r) in izip(overlap(first),paired(second)): 
      for item in dopair(i,r): 
       yield (pa,) + item 

def generator(arr): 
    for pa,ir in paired(arr): 
     for x in dopair(pa,ir): 
      yield x 

for x in generator(arr): 
    print x