码迷,mamicode.com
首页 > 编程语言 > 详细

python拆分整型字符串并转为整型list

时间:2020-02-08 19:21:34      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:表达式   python   www   pytho   print   ram   整型   list   空字符串   

python正则表达式模块,拆分字符串,

re.split()eg:s = ‘1, 2, 3, 4‘

拆分组成数字list:strs = re.split(‘, ‘, s);

print(strs);

结果:[‘1‘, ‘2‘, ‘3‘, ‘4‘]

转成int行list:strs = list(map(int, strs));

print(strs);

结果:[1, 2, 3, 4]

如果strs中有多个分隔符,

模式串中间应用“|”分隔

各个分隔字符(或分隔字符串):eg:strs = ‘[112, 236, 372; 131]‘;

strs = re.split(‘, |; |||‘);print(strs);

结果: [‘‘, ‘112‘, ‘236‘, ‘372‘, ‘131‘, ‘‘]

去除首尾空字符串:strs.remove(‘‘); 

####一次只能去除一个空串

参考:

http://www.jb51.net/article/85001.htm

https://blog.csdn.net/programmer_at/article/details/77409507?locationNum=7&fps=1

 

python拆分整型字符串并转为整型list

标签:表达式   python   www   pytho   print   ram   整型   list   空字符串   

原文地址:https://www.cnblogs.com/lbwnb/p/12284440.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!