2017-10-18 3 views
-1

나는 다음과 같은 목록을 가지고 상상 :연결하여 목록 요소

while elements remain in mylist: 
    # Find the next "DT" element 
    look at next element 
    while element[1] is not "DT": 
     look at next element 

    # Watch for "IN"; concatenate elements in the meantime. 
    result = [] 
    look at next element 
    while element[1] is not "IN": 
     result.append(element[0]) 

    # result is a list of the words you want 
    result_conc = ' '.join(result) 
: 여기
[('Satisfactory tracing'), 
('fairly persistent')] 
+0

@engr_s 결과의 두 번째 요소입니다.'(상당히 영구적입니다. ') – Barmar

+0

itertools.dropwhile() 및 itertools.takewhile()을 사용해보십시오. –

답변

1

여기에 원하는 결과가 표시됩니다. 어쩌면 최적화 할 필요가 있습니다.

my_list = ([('a', u'DT'), 
      ('Satisfactory', u'JJ'), 
      ('tracing', u'VBG'), 
      ('with', u'IN'), 
      ('a', u'DT'), 
      ('fairly', u'RB'), 
      ('persistent', u'JJ'), 
      ('with', u'IN')]) 

sequence_enable = False 
new_list = [] 
for i in my_list: 
    if i[1] == 'DT' or i[1] == 'IN': 
     if not sequence_enable: # Start reading values 
      sequence_enable = True 
      temp_str = [] 
     else: # stop reading values 
      new_list.append(' '.join(temp_str)) 
      sequence_enable = False 

     continue 
    else: # store values 
     if sequence_enable: 
      temp_str.append(i[0]) 

print(new_list) 
# output: ['Satisfactory tracing', 'fairly persistent'] 
+0

fantastic - 저에게 도움이되며 새 분할 태그에 쉽게 추가 할 수 있습니다. 감사. 이것에 대해 – brucezepplin

0

은 일부 의사 코드와 실제 코드의

이사 하시겠습니까?

1

이것은 하나의 해결책이다

idt = [item for item in range(len(mylist)) if mylist[item][1] == u'DT'] 
jdt = [item for item in range(len(mylist)) if mylist[item][1] == u'IN'] 

ij = zip(idt,jdt) 

temp_list = [mylist[i[0]+1:i[1]] for i in ij] 

new_list = [str(elem[0][0]+ ' ' + elem[1][0]) for elem in temp_list] 

mylistu'DT'u'IN'의 인덱스를 찾는, 그때는 튜플들의 목록 ij에 이들을 함께 참아. 각 튜플은 프로그램이 값을 추출하기로되어있는 간격의 시작과 끝입니다. 값은 mylist에 표시된대로 temp_list으로 먼저 추출됩니다. 마지막 단계는 에 저장된 추출 값을 조인하고 처리하는 대상 new_list을 형성합니다.

이 솔루션은 당신에게 괄호 () 내에서 값을 제공하지 않습니다 - 그이를 것으로 보인다 new_list의 요소, 튜플로 전환해야 할 것 중 하나 즉 ('Satisfactory tracing',) 또는 브래킷 문자열 '(Satisfactory tracing)'의 일부가 될 것이다.

수정 - DTIN 사이 문자열의 수를위한 용액 - new_list이 동일 할 때까지 모든 것을 new_listtemp_list의 하위 목록의 각 요소에서의 첫 번째 요소를 추출하고, 하나의 문자열로 접합하여 형성되어

new_list = [((' ').join(map(lambda x: x[0], sub_el))) for sub_el in temp_list] 

mylist = [('a', u'DT'), ('Satisfactory', u'JJ'), ('Satisfactory', u'JJ'), 
('tracing', u'VBG'),('with', u'IN'),('a', u'DT'),('fairly', u'RB'), 
('persistent', u'JJ'),('with', u'IN'), ('a', u'DT'),('persistent', u'JJ'), 
('with', u'IN')] 

테스트

을 수득 0
['Satisfactory Satisfactory tracing', 'fairly persistent', 'persistent'] 
+0

감사합니다. new_list를 만드는 마지막 명령은 목록 색인이 범위를 벗어나고 있다고 불평합니다. 나는 이것이 'temp_list'가 대부분 빈 요소를 포함하고 있기 때문이라고 생각한다. 나는'temp_list'가'mylist'에서 원소 asis를 집어 내길 원했음을 알 수 있습니다. 그러나 이것은 일어나지 않았고 이것은 에러를 일으킨다 고 생각합니다. – brucezepplin

+0

도와 드리겠습니다. 나에게 아무런 오류도 없었습니다.이 모든 것은 당신이 제공 한 정확한 예를 통해 확인되었습니다. 오류의 원인이되는 예제를 게시 할 수 있습니까? 또한'temp_list'는 전혀 비어 있지 않습니다. – atru

+0

'DT'와 'IN'사이에 두 개의 요소가있는 경우에만 유효합니다. – atru