打印每个列表的第一个元素的Pythonic方法是什么?
例如,我想从下面的列表列表中['apple', 'banana']
:
data = [['apple','airplane'],['banana','boat']]
这是我最好的尝试:
fruit = [list(fruit) for fruit in data]
[letter[0] for letter in fruit]
然而,有两个列表推导似乎并不是非常pythonic
答案 0 :(得分:0)
为什么不
fruit = [fruit[0] for fruit in data]
这让我获得了预期的输出,见下文:
>>> data = [['apple','airplane'],['banana','boat']]
>>> fruit = [fruit[0] for fruit in data]
>>> print fruit
['apple', 'banana']