Home > Article > Backend Development > Python explains how to implement heavy operations on strings
String deduplication is a common requirement for string operations in python. I have encountered it again at work recently, so the following article mainly introduces you to the relevant information about Python's implementation of deduplication operations on strings. The article gives a detailed introduction. Friends who need it can refer to it. Let’s take a look together.
Preface
Recently, I often encounter the deduplication operation on strings at work. Here is a list of how to handle it using Python. Yes, not much to say, let’s take a look at the detailed introduction.
For example, take the following characters and remove the repeated AA, A(B,C)
##S = 'AA, BB, EE, DD, AA , A(B,C), CC, A(B,C)'
##The code is as follows:
Remarks:## 1. Use
str.split(',')
re.split(',|:') 2. The original string is separated by commas separated, followed by one or more strings, so
re.split(', | ')
## 3. Execute
re.split(r', | ', S)
After the operation, a large number of '' will be generated in the list, and you need to filter it out
4. Use
L.count(x) == 1
or
to retain duplicates or non-duplicates 5.
set(L)
then is to retain the only item in the list, and then use
to convert it into a list 6. Use
', '.join(L)
, splice the list into the string we want
Summary
With the help of regular expressions (re) in python , and operations such as lists, strings, and sets, it is still very flexible to process strings!
The above is the detailed content of Python explains how to implement heavy operations on strings. For more information, please follow other related articles on the PHP Chinese website!