Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将嵌套列表转换为int或str_Python_Python 3.x_List - Fatal编程技术网

Python 将嵌套列表转换为int或str

Python 将嵌套列表转换为int或str,python,python-3.x,list,Python,Python 3.x,List,我有以下列表结构: the_given_list = [[[1],[2],[3]],[[1],[2],[3]]] 实际上len(给定列表)返回2。 我需要列出以下清单: the_given_list = [[1,2,3],[1,2,3]] 怎么做 [sum(x, []) for x in the_given_list] 展平给定列表中的一阶元素 the_given_list = [sum(x, []) for x in the_given_list] print(the_given_li

我有以下列表结构:

the_given_list = [[[1],[2],[3]],[[1],[2],[3]]]
实际上
len(给定列表)
返回2。 我需要列出以下清单:

the_given_list = [[1,2,3],[1,2,3]]
怎么做

[sum(x, []) for x in the_given_list]

展平
给定列表中的一阶元素

the_given_list = [sum(x, []) for x in the_given_list]
print(the_given_list)

展平
给定列表中的一阶元素

the_given_list = [sum(x, []) for x in the_given_list]
print(the_given_list)

要解释上述答案,请参阅以下列表

[[[1],[2],[3]],[[1],[2],[3]]]
可视为

[list1, list2]
以及

由于方法
sum
的第二个参数默认为
0
,因此我们需要显式地将空列表
[]
传递给它,以克服类型不匹配(int和list之间)


要解释上述答案,请查看以下列表

[[[1],[2],[3]],[[1],[2],[3]]]
可视为

[list1, list2]
以及

由于方法
sum
的第二个参数默认为
0
,因此我们需要显式地将空列表
[]
传递给它,以克服类型不匹配(int和list之间)

另一种解决方案:

the_given_list = [[[1],[2],[3]],[[1],[2],[3]]]
print([[j for sub in i for j in sub] for i in the_given_list])
给我:

[[1, 2, 3], [1, 2, 3]]
检查列表展平的原始答案:

另一种解决方案:

the_given_list = [[[1],[2],[3]],[[1],[2],[3]]]
print([[j for sub in i for j in sub] for i in the_given_list])
给我:

[[1, 2, 3], [1, 2, 3]]
检查列表展平的原始答案:

使用

使用


@如果对我有用的话。哪一部分不适合你?如果适合我。哪一部分不适合您?可能重复的可能重复的