如何转换list1中的list = [1,2,[3,4],[5,6],7,[8,9,10]] = [1,2,3,4,5,6,7,8 ,9,10]在python中?

塞尔戈

我需要将转换列表转换为“正常”列表

list = [1,2,[3,4],[5,6],7,[8,9,10]]

list = [1,2,3,4,5,6,7,8,9,10]

斯蒂芬·布里尼

这是我的答案。首先,不要将您的列表称为“列表”。这使我们无法使用此答案所需的内置列表关键字。

import collections
from itertools import chain

#input list called l
l = [1,2,[3,4],[5,6],7,[8,9,10]]

#if an item in the list is not already a list (iterable) then put it in one. 
a = [i if isinstance(i, collections.Iterable) else [i,] for i in l]

#flattens out a list of iterators
b = list(chain.from_iterable(a))

print b
#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python:将1,2,3-7,8,9,10变成这样的字符串1,2,3,4,5,6,7,8,9,10

为什么n = [1,2,3,4,5,6,7,8],n [:6:-2]在Python中是[8]?

如何重复序列:r中的1,2,3,4,5,6,1,2,3,4,5,6,7,8,9,10,7,8,9,10

我希望输出像 [[1,2,3,4,5,6,7,8,9,10],[2,4,6,8,10,12...18,20] ,[3,6,9...27,30].....[9,18,27..90]]

我想把数组 [1,2,3,4,5,6,7,8,9,10,11] 变成数组 [1,2,3,4,5,6,7,8,9,1 ,0,1,1] 仅使用此算法

重新排列 PHP 数组,将 1、2、3、4、5、6、7、8、9 重新排序为 1、4、7、2、5、8、3、6、9

C中{0,1,2,3,4,5,6,7,8,9}外的条件

在用户键入(1 2 3 4 5 6 7 8 9 10)时无法显示正确的最大值

将数组的元素添加为a [0],a [1] + a [2],a [3] + a [4] + a [5],a [6] + a [7] + a [8] + a [9] ...等等

Javascript - 找到 1-4-7, 2-5-8, 3-6-9 iretartions?

批处理文件中cp%1%2%3%4%5%6%7%8%9的含义是什么

生成此序列的通用函数:R中的前n = 100个数字为1 4 3 6 5 8 7 10 9 12

我如何设计一个以1开始并以0(1,2,3,4,5,6,7,8,9,0)结束的int循环

我想通过 python selenium 库显示 **href** 上下文 1,2,3,4,5,6,7,8,9

清除算法以生成类型为(0)到(0,1,2,3,4,5,6,7,8,9)的所有集合

拆分数据时出现问题:KeyError: "None of [Int64Index([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], dtype='int64')] are in [列]"

将1,2,3,4,5,6,8,10,11显示为1-6,8,10-11

如果display(15)我想要结果1,2,3,4,5,6,8,9,10,12,15,16,18,20,24

使用R中的四对(1,2),(3,4),(5,6),(7,8)中的三对进行6个数字的组合

R:如何制作序列(1,1,1,2,3,3,3,4,5,5,5,6,7,7,7,8)

如何在Java中合并List <Date> list1和List <Date> list2?

while循环| 反向计数n .... 8 7 6 5 4 3 2 1

如何使用 Angular 2、4、5、6、7、8、9 创建层次结构

list1 == list2.reverse() 如何在python中执行?

将在 for 循环中创建的所有变量视为向量,例如 `c(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10)`

在Java中找到1=>5的函数;2=>4;3=>3; 4=>2;5=>1;6=>7;7=>6;

下划线或lazy.js映射(0,1,2,3,4)+(1,2,3,4,5)->(1,3,5,7,9)

1 2 3 4 7 8 9 p和退格键不起作用

Javascript:根据模式拆分数组:项1、5、10、2、6、11、3、7、12