且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在列表中的每个列表中列出特定位置(python)

更新时间:2023-11-28 16:37:40

无任何循环(外部)

>>> f = [["1", "5", "8", "9"], ["2", "6", "9", "10"], ["3", "7", "11", "12"]]
>>> list(map(lambda x:x[1],f))  # In python2, The list call is not required
['5', '6', '7']

Ref: map

Ref : map

另一种无循环的方式(礼貌:

Another way to do it without loop (Courtesy : Steven Rumbalski)

>>> import operator
>>> list(map(operator.itemgetter(1), f))
['5', '6', '7']

参考: itemgetter

另一种无循环的方法(礼貌:

Yet another way to do it without loop (Courtesy : Kasra A D)

>>> list(zip(*f)[1])
['5', '6', '7']

参考: zip