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

Sec*_*mon 1 python position list matrix python-3.x

有没有办法在矩阵中选择每个第二个或第三个(例如)项目?

例如:

f = [["1", "5", "8", "9"], ["2", "6", "9", "10"], ["3", "7", "11", "12"]]
Run Code Online (Sandbox Code Playgroud)

我想知道是否有一个直接的功能来选择每个列表中的每个第二个数字(最好也将这些数字放在一个列表中).从而导致:

["5", "6", "7"]
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用循环实现这一点,但我想知道我是否可以直接实现这一点.

Bha*_*Rao 5

没有任何循环(外部)

>>> 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']
Run Code Online (Sandbox Code Playgroud)

参考: map

另一种没有循环的方法(礼貌:Steven Rumbalski)

>>> import operator
>>> list(map(operator.itemgetter(1), f))
['5', '6', '7']
Run Code Online (Sandbox Code Playgroud)

参考: itemgetter

还有另一种没有循环的方法(图片来源:Kasra AD)

>>> list(zip(*f)[1])
['5', '6', '7']
Run Code Online (Sandbox Code Playgroud)

参考: zip