2012-12-17 3 views
0

다음 중첩 for 루프를리스트 이해로 변환하는 데 도움이 필요합니다. 가장자리리스트 변환 파이썬으로 변환하기

adj_edges = [] 
for a in edges: 
    adj = [] 
    for b in edges: 
     if b[0] < a[0] and b[1] >= a[0] and b[1] <= a[1]: 
      adj.append(b) 
    adj_edges.append((a[0], adj)) 

이 같은 목록의 목록입니다 [0, 200], [200, 400] 나는 전에 지능형리스트를 사용했습니다하지만 난 몰라 이유 중 하나에 문제가 메신저 . 여기

답변

0

그것입니다

>>> edges = [[0, 200], [200, 400]] 
>>> adj_edges = [] 
>>> for a in edges: 
...  adj = [] 
...  for b in edges: 
...   if b[0] < a[0] and b[1] >= a[0] and b[1] <= a[1]: 
...    adj.append(b) 
...  adj_edges.append((a[0], adj)) 
... 
>>> adj_edges 
[(0, []), (200, [[0, 200]])] 
>>> [(a[0], [b for b in edges if b[0] < a[0] and b[1] >= a[0] and b[1] <= a[1]]) for a in edges] 
[(0, []), (200, [[0, 200]])] 
:

adj_edges = [(a[0], [b for b in edges if b[0] < a[0] and b[1] >= a[0] and b[1] <= a[1]]) for a in edges] 

이이 두 가지 대체 방법을 보여주는 대화 형 세션

관련 문제