Python splits an integer string and transitions to an integer list

Source: Internet
Author: User

Python regular expression module, split string, Re.split ()

eg

s = ' 1, 2, 3, 4 '

Split up the list of numbers:

STRs = Re.split (', ', s);

Print (STRs);

Results: [' 1 ', ' 2 ', ' 3 ', ' 4 ']

Go to int line list:

STRs = list (map (int, strs));

Print (STRs);

Results: [1, 2, 3, 4]

If there is more than one delimiter in the STRs, the middle of the pattern string applies the ' | ' Separate delimited characters (or delimited strings):

eg

STRs = ' [112, 236, 372; 131] ';

STRs = Re.split (', |; |\[|\] ');

Print (STRs);

Result: [' ', ' 112 ', ' 236 ', ' 372 ', ' 131 ', ']

To remove the null and void string:

Strs.remove ("); # # #一次只能去除一个空串

Reference:

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

77409507?locationnum=7&fps=1

Python splits an integer string and transitions to an integer list

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.