Code example of how to implement string deduplication in Python

黄舟
Release: 2017-08-11 14:05:46
Original
2820 people have browsed it

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(',')

can only be separated by commas; if multiple separations are involved, you need to use

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

L.count(x) > 1

to retain duplicates or non-duplicates 5. set(L)
then is to retain the only item in the list, and then use

list()

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 Code example of how to implement string deduplication in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template