简体   繁体   English

Python 将字符串数字序列转换为整数列表

[英]Python Convert sequence of string numbers to list of integers

I have a sequence of numbers such as 1-3-6-16-20 I need to be able to convert these to a list of numbers such as [1,3,6,16,20]我有一个数字序列,例如 1-3-6-16-20 我需要能够将它们转换为数字列表,例如 [1,3,6,16,20]

I've tried to make a regex to substitute the hypen to a comma and add in the square brackets around it, but it doesn't work我试图制作一个正则表达式来将连字符替换为逗号并在它周围添加方括号,但它不起作用

def stringToInt(value):
    reformat = re.sub(r'\-', r',', value)
    reformat = [reformat]
    return reformat

It gives me ['1,3,6,16,20']它给了我 ['1,3,6,16,20']

I have a sequence of numbers such as 1-3-6-16-20 I need to be able to convert these to a list of numbers such as [1,3,6,16,20]我有一个数字序列,例如 1-3-6-16-20 我需要能够将它们转换为数字列表,例如 [1,3,6,16,20]

I've tried to make a regex to substitute the hypen to a comma and add in the square brackets around it, but it doesn't work我试图制作一个正则表达式来将连字符替换为逗号并在它周围添加方括号,但它不起作用

def stringToInt(value):
    reformat = re.sub(r'\-', r',', value)
    reformat = [reformat]
    return reformat

It gives me ['1,3,6,16,20']它给了我 ['1,3,6,16,20']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM