仅包含元组的单个列表最简单的是:
[x[1] for x in myList]
# [None, None, None, None]
或者如果它总是元组中的最后一个值(如果它包含两个以上的值):
[x[-1] for x in myList]
# [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]]
或其他一些变体:
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]]
另请参阅:
SO:了解嵌套列表理解