Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.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中将数字字符串转换为整数列表_Python_List - Fatal编程技术网

在python中将数字字符串转换为整数列表

在python中将数字字符串转换为整数列表,python,list,Python,List,我有一个字符串12345678,我想把它转换成python中的列表[1,2,3,4,5,6,7,8] 我试过这个方法: 您可以使用地图: list(map(int, '12345678')) # [1, 2, 3, 4, 5, 6, 7, 8] 或列表: [int(x) for x in '12345678'] # [1, 2, 3, 4, 5, 6, 7, 8] 如果希望不使用循环或贴图,可以尝试: final_=[] def recursive(string1): if no

我有一个字符串12345678,我想把它转换成python中的列表[1,2,3,4,5,6,7,8]

我试过这个方法:

您可以使用
地图

list(map(int, '12345678'))  # [1, 2, 3, 4, 5, 6, 7, 8]
或列表:

[int(x) for x in '12345678']  # [1, 2, 3, 4, 5, 6, 7, 8]

如果希望不使用循环或贴图,可以尝试:

final_=[]
def recursive(string1):
    if not string1:
        return 0
    else:
        final_.append(int(string1[0]))
        return recursive(string1[1:])
recursive('12345678')
print(final_)
输出:

[1, 2, 3, 4, 5, 6, 7, 8]

代码中的问题似乎是使用逗号进行拆分,但没有输入这样的数字:
“1,2,3,4”
映射(int,'12345678')的可能重复就足够了。