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!