[英]How to divide a 2D list into one 2D list and one array (remove the last column)
I have the following 2D list 我有以下2D清单
list = [[1,1,a],[2,2,b],[3,3,c]]
and I want to convert this 2D list to one 2D list and an array 我想将此二维列表转换为一个二维列表和一个数组
sublist = [[1,1],[2,2],[3,3]]
subarray = [a,b,c]
Is there any convenient way to do that in python. 有什么方便的方法可以在python中做到这一点。 I'm new to python, so I dont know if there's any grammar which can perform the above calculation.
我是python的新手,所以我不知道是否有任何语法可以执行上述计算。
Try this: 尝试这个:
lst = [[1,1,a],[2,2,b],[1,3,c]]
sublist = list(map(lambda x: [x[0],x[1]], lst))
subarray = list(map(lambda x: x[2], lst))
List comprehensions are a good, clean solution: 列表理解是一个很好的干净解决方案:
sublist = [[a[0], a[1]] for a in list]
subarray = [a[2] for a in list]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.