提取每个子列表的第一项 [英] Extract first item of each sublist
本文介绍了提取每个子列表的第一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道提取列表列表中每个子列表的第一项并将其附加到新列表的最佳方法是什么?所以,如果我有
I am wondering what is the best way to extract the first item of each sublist in a list of lists and append it to a new list. So if I have:
lst = [[a,b,c], [1,2,3], [x,y,z]]
,我想拉出a
,1
和x
,并从中创建一个单独的列表.
and I want to pull out a
, 1
and x
and create a separate list from those.
我尝试过:
lst2.append(x[0] for x in lst)
推荐答案
使用列表理解:
>>> lst = [['a','b','c'], [1,2,3], ['x','y','z']]
>>> lst2 = [item[0] for item in lst]
>>> lst2
['a', 1, 'x']
这篇关于提取每个子列表的第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文