Python将字符串数字序列转换为整数列表
我有一个数字序列,比如1-3-6-16-20 我需要能够将这些转换为数字列表,例如[1,3,6,16,20] 我试着制作一个正则表达式,将hypen替换为逗号,并在其周围的方括号中添加,但它不起作用Python将字符串数字序列转换为整数列表,python,string,list,int,Python,String,List,Int,我有一个数字序列,比如1-3-6-16-20 我需要能够将这些转换为数字列表,例如[1,3,6,16,20] 我试着制作一个正则表达式,将hypen替换为逗号,并在其周围的方括号中添加,但它不起作用 def stringToInt(value): reformat = re.sub(r'\-', r',', value) reformat = [reformat] return reformat 它给了我['1,3,6,16,20']例如[int(x)表示“1-3-6-
def stringToInt(value):
reformat = re.sub(r'\-', r',', value)
reformat = [reformat]
return reformat
它给了我['1,3,6,16,20']例如[int(x)表示“1-3-6-16-20”中的x。拆分('-')]
def StringTOINt(values):
res = [int(int_values) for int_values in values.split('-')]
return res
res = StringTOINt("1-3-6-16-20")
print(res)