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