Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.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中将[1,2,3]转换为[1,2,3]?_Python - Fatal编程技术网

在python中将[1,2,3]转换为[1,2,3]?

在python中将[1,2,3]转换为[1,2,3]?,python,Python,我试过: my_list = ["1, 2, 3", "4, 5, 6"] new_list = [] for e in my_list: for sub_e in e: new_list.append(int(sub_e)) 但我认为它是在迭代1、、2、,等等。。。如何将[“1,2,3”]转换为整数列表[1,2,3] 我想要的输出是一个整数列表:[1,2,3]试试: my_list = ["1, 2, 3", "4, 5, 6"] new_list = []

我试过:

my_list = ["1, 2, 3", "4, 5, 6"]

new_list = []

for e in my_list:
    for sub_e in e:
        new_list.append(int(sub_e)) 
但我认为它是在迭代
1、
2、
,等等。。。如何将
[“1,2,3”]
转换为整数列表
[1,2,3]

我想要的输出是一个整数列表:[1,2,3]

试试:

my_list = ["1, 2, 3", "4, 5, 6"]

new_list = []

for e in my_list:
    for sub_e in e.split(","):
        new_list.append(int(sub_e)) 

您想要的输出是什么?请注意第0个元素,我们正在做的是:
my_list[0]。拆分(“,”
)。如果我打印这个,我会得到
[“1”、“2”、“3”]
。既然
“1,2,3”中的每个元素都是
1,
或者是一行肮脏的代码:
list(map(int,sum(map(lambda x:x.split(','),my_list)),[]),
我更喜欢这个:
[int(I)代表我在','中的I。join(my_list)。split(','))][/code>@usercode>@usercode(','):尝试<3138766(“,”),我的代码(“,”),
split
方法不会将分隔符字符串保留在片段中,(那么分隔符是一个空格,我认为这是不正确的)我最喜欢的一行是:
new_list=[int(sub_e)for e in My_list for e for sub_e in e.split(',')]