方法一:使用列表推导式
vec = [[1,2,3],[4,5,6],[7,8,9]]
get = [num for elem in vec for num in elem]
get
[1, 2, 3, 4, 5, 6, 7, 8, 9]
方法相当于
vec = [[1,2,3],[4,5,6],[7,8,9]]
result = []
for elem in vec:
for num in elem:
result.append(num)
result
[1, 2, 3, 4, 5, 6, 7, 8, 9]
方法二:
使用sum函数
vec = [[1,2,3],[4,5,6],[7,8,9]]
get = sum(vec,[])
get
[1, 2, 3, 4, 5, 6, 7, 8, 9]
或者是使用chain函数
vec = [[1,2,3],[4,5,6],[7,8,9]]
from itertools import chain
list(chain(*vec))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
————————————————
版权声明:本文为CSDN博主「guo_ridgepole」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/guo_ridgepole/article/details/79953979
网友评论