Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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/1/list/4.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
Arrays 字符串到整数数组_Arrays_List_Python 3.x_Integer - Fatal编程技术网

Arrays 字符串到整数数组

Arrays 字符串到整数数组,arrays,list,python-3.x,integer,Arrays,List,Python 3.x,Integer,我有一个字符串'[1.2.3.4.5.],我想转换为只获取int,这样我就获得了一个整数数组[1,2,3,4,5] 我该怎么做?我尝试使用map,但没有成功。使用strip删除[],使用拆分转换为列表理解中的值的列表: s = '[1. 2. 3. 4. 5.]' print ([int(x.strip('.')) for x in s.strip('[]').split()]) [1, 2, 3, 4, 5] 类似的解决方案是使用替换来删除: s = '[1. 2. 3. 4. 5.]' p

我有一个字符串
'[1.2.3.4.5.]
,我想转换为只获取int,这样我就获得了一个整数数组
[1,2,3,4,5]


我该怎么做?我尝试使用
map
,但没有成功。

使用
strip
删除
[]
,使用
拆分
转换为
列表理解中的
值的
列表

s = '[1. 2. 3. 4. 5.]'
print ([int(x.strip('.')) for x in s.strip('[]').split()])
[1, 2, 3, 4, 5]
类似的解决方案是使用
替换
来删除

s = '[1. 2. 3. 4. 5.]'
print ([int(x) for x in s.strip('[]').replace('.','').split()])
[1, 2, 3, 4, 5]
或者先转换为
float
,然后再转换为
int

s = '[1. 2. 3. 4. 5.]'
print ([int(float(x)) for x in s.strip('[]').split()])
[1, 2, 3, 4, 5]
使用
映射的解决方案

s = '[1. 2. 3. 4. 5.]'
#add list for python 3
print (list(map(int, s.strip('[]').replace('.','').split())))
[1, 2, 3, 4, 5]

使用
条带
删除
[]
拆分
转换为
列表
中转换为
int

s = '[1. 2. 3. 4. 5.]'
print ([int(x.strip('.')) for x in s.strip('[]').split()])
[1, 2, 3, 4, 5]
类似的解决方案是使用
替换
来删除

s = '[1. 2. 3. 4. 5.]'
print ([int(x) for x in s.strip('[]').replace('.','').split()])
[1, 2, 3, 4, 5]
或者先转换为
float
,然后再转换为
int

s = '[1. 2. 3. 4. 5.]'
print ([int(float(x)) for x in s.strip('[]').split()])
[1, 2, 3, 4, 5]
使用
映射的解决方案

s = '[1. 2. 3. 4. 5.]'
#add list for python 3
print (list(map(int, s.strip('[]').replace('.','').split())))
[1, 2, 3, 4, 5]