, 당신은 당신의 목록을 통해 하나의 패스로이 작업을 수행 할 수 있어야한다 (다음 인덱스를 시작 ID로 분류 가정).
여기에 가정이있다. 그냥 직전이라면
import itertools, operator
def find_t1_t2(seq):
"""Find every pair of type1, type2 values where the type1 occurs
before the type2 within a block with the same id.
Assumes sequence is ordered by id, then start location.
Generates a sequence of tuples of the type1,type2 entries.
"""
for group, items in itertools.groupby(seq, operator.itemgetter(0)):
type1s=[]
for item in items:
if item[1] == TYPE1:
type1s.append(item)
elif item[1] == TYPE2:
for t1 in type1s:
yield t1 + item[1:]
, 그것도 간단 : 두 번째 옵션 (모든 쌍)을 보내고 단지 이전 항목을 추적하고 타입 1이며, 현재는 타입 2 때마다 튜플을 얻을 수 있습니다.
다음은 사용의 예, 그 결과를 반환
l=[[1, TYPE1, 10, 15],
[1, TYPE2, 20, 25], # match with first
[1, TYPE2, 30, 35], # match with first (2 total matches)
[2, TYPE2, 10, 15], # No match
[2, TYPE1, 20, 25],
[2, TYPE1, 30, 35],
[2, TYPE2, 40, 45], # Match with previous 2 type1s.
[2, TYPE1, 50, 55],
[2, TYPE2, 60, 65], # Match with 3 previous type1 entries (5 total)
]
for x in find_t1_t2(l):
print x
이 반환
[1, 'type1', 10, 15, 'type2', 20, 25]
[1, 'type1', 10, 15, 'type2', 30, 35]
[2, 'type1', 20, 25, 'type2', 40, 45]
[2, 'type1', 30, 35, 'type2', 40, 45]
[2, 'type1', 20, 25, 'type2', 60, 65]
[2, 'type1', 30, 35, 'type2', 60, 65]
[2, 'type1', 50, 55, 'type2', 60, 65]
어떻게 텍스트의 블록에 할당 된 ID를입니까? 이를 알고 있으면 효율적인 알고리즘을 개발하는 데 도움이됩니다. – Kai
유형 1이 유형 2보다 먼저 결정됩니까? 단순히 유형 1 시작 유형 2 시작입니까? – mamboking
빠른 접근 방법을 찾는 것은 사물의 모양에 다소 의존합니다. 많은 ID가 있습니까? 각 ID마다 동일한 유형이 여러 번 또는 여러 번 있습니까? – tom10