Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

How can I remove duplicate string with Python?

I am to learn python, I have a problem to deal with Following example:

string1 = "Galaxy S S10 Lite"
string2 = "Galaxy Note Note 10 Plus"

How can I remove the second two duplicates "S" and "S" or "Note" and "Note"?

The result should look like

string1a = "Galaxy S10 Lite"
string2a = "Galaxy Note 10 Plus"

how to only the second duplicates should be removed and the sequence of the words should not be changed!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
string1a = string1.split()
del string1a[1]
string1a = " ".join(string1a)

This does what you want for the 2 strings provided. It will only work in all the strings you want, if you know for sure that the second and third words of the string are the duplicates, giving preference to the third one.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...