간단한 단 하나의 목록이 포함 된 튜플이 될 것이기 :
[x[-1] for x in myList]
# [None, None, None, None]
참고 :
[x[1] for x in myList]
# [None, None, None, None]
또는을 (이 두 개 이상의 값을 포함하는 경우) 항상 튜플의 마지막 값의 경우 아래의 예제는 중첩 목록을 사용하고 있습니다. 튜플을 포함하는 목록입니다. 나는 당신이 목록의 두 가지 변형을 보여 주었던 이래로 당신이 찾고 있던 것이라고 생각했습니다.
중첩 된 이해 목록 사용을 참조 또한
myList =[ [(None, None), (' mated', None), (' rooster', None), ('', None)] ,
[(' produced', None), (' without', None), (' rooster', None), (' infertile', None), ('', None)] ]
# If there are multiple none values (if the tuple isn't always just two values)
print [ [ [ x for x in z if x == None] for z in el ] for el in myList ]
# [[[None, None], [None], [None], [None]], [[None], [None], [None], [None], [None]]]
# If it's always the last value in the tuple
print [[x[-1] for x in el] for el in myList]
# [[None, None, None, None], [None, None, None, None, None]]
:
myList =[ [(' whether', None), (' mated', None), (' rooster', None), ('', None)] ,
[(' produced', None), (' without', None), (' rooster', None), (' infertile', None), ('', None)] ]
print [[x[1] for x in el] for el in myList]
# [[None, None, None, None], [None, None, None, None, None]]
또는 다른 변화를 SO: Understanding nested list comprehension
실제리스트 (우리가 인터프리터에 입력 할 수있는 파이썬 목록 디스플레이)를 보여줄 수 있습니까? 그것 [428, [(...)], 429, [(...)], ...)'입니까? 아니면 다른 무엇입니까? – abarnert
예제에서 "없음"은 항상 튜플의 두 번째 값입니다. 그럴 경우 [myList의 x에 대해 x [1]을 할 수 있습니다.] –
또한 'tuple' 만 검색 할 수있는 목록 이해력은 무엇입니까? 표현의 끝 부분에'[1]'을 붙이지 않아도되는 이유가 있을까요? – abarnert