python 如何将a = [[1,2,3], [4,5,6], [7,8,9]]列表中的元素变成a=[1,2,3,4,5,6,7,8,9]
2个回答
展开全部
方法1:
>>> a = [[1,2,3], [4,5,6], [7,8,9]]
>>> a=[num for elem in a for num in elem]
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9]
方法2:
>>> a = [[1,2,3], [4,5,6], [7,8,9]]
>>> a= sum(a,[])
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9]
方法3:
>>> a = [[1, 2, 3],[4, 5,6],[7,8,9]]
>>> from itertools import chain
>>> a =list(chain(*a))
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a = [[1,2,3], [4,5,6], [7,8,9]]
>>> a=[num for elem in a for num in elem]
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9]
方法2:
>>> a = [[1,2,3], [4,5,6], [7,8,9]]
>>> a= sum(a,[])
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9]
方法3:
>>> a = [[1, 2, 3],[4, 5,6],[7,8,9]]
>>> from itertools import chain
>>> a =list(chain(*a))
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9]
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询