首页 > 编程语言 > 详细

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

时间:2018-05-18 19:42:49      阅读:321      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/HITSZ/p/9057600.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!