gpt4 book ai didi

python - 从列表列表中获取不连续的列

转载 作者:太空宇宙 更新时间:2023-11-04 10:56:31 24 4
gpt4 key购买 nike

有没有什么方法可以使用理解从 python 列表的列表中获取不连续的列?我的意思是,如果我们有一个列表:

a = [[1,2,3],[4,5,6]]

我想要第一列和第三列,即:

[[1,3],[4,6]]

我可以用类似于以下的句子得到该列表吗:

[row[1,3] for row in a]

??

感谢您的帮助!

最佳答案

>>> a = [[1,2,3],[4,5,6]]
>>> from operator import itemgetter
>>> map(itemgetter(0,2), a)
[(1, 3), (4, 6)]
>>>

或作为列表理解

>>> [itemgetter(0,2)(i) for i in a]
[(1, 3), (4, 6)]

关于python - 从列表列表中获取不连续的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9394051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com