Python explains how to re-manipulate string implementations

Source: Internet
Author: User
String de-weight is a common requirement in Python string operations, and recently encountered in the work, so the following this article mainly introduces the python on the implementation of the string to re-operation of the relevant information, the text gives a detailed introduction, the need for friends can reference, the following to see together.

Objective

Recently in the work will often encounter the string to go to re-operation, the following gives you a list of how to deal with Python, not much to say, come together to see the detailed introduction.

For example, to take the following word nginx remove the duplicate AA, A (b,c)

S = 'AA, BB, EE, DD, AA, A(B,C), CC, A(B,C)'

The code is as follows:


Note:

1. str.split(',') only one comma can be separated, and if multiple separators are involved, you need to usere.split(',|:')

2. The original string is separated by commas, followed by one or more strings, sore.split(', | ')

3. After performing the re.split(r', | ', S) operation, the list will generate a lot of ", you need to filter out

4. Use L.count(x) == 1 or L.count(x) > 1 to retain duplicates or, non-duplicates

5. set(L) keep the unique item in the list, then list() convert it to a list

6. Use ', '.join(L) to stitch the list into the string we want

Summarize

With the help of regular expressions (re) in Python, and the manipulation of lists, strings, collections, and so on, it's very flexible to handle strings!

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.