将字符串转换为包含负数的整数的列表 [英] Convert a string into a list with integers including negative numbers
本文介绍了将字符串转换为包含负数的整数的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要转换此字符串
-1, -2, 3, 4, 5
进入具有这些值的列表.
into a list with those values.
[-1, -2, 3, 4, 5]
当值是正数时,我有很多选择,但是一旦涉及到-"字符,我似乎就无法获得它.
When the values are positive, I have many options but I can't seem to get it once there is a "-" character involved.
如果可能的话,我不想使用正则表达式.
If possible, I'd like not to use regex.
推荐答案
string = "-1, -2, 3, 4, 5"
print [int(el) for el in string.split(',')]
输出:
[-1, -2, 3, 4, 5]
这篇关于将字符串转换为包含负数的整数的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文