python拆分整型字符串并转为整型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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM