How can I remove duplicate characters from a string using Python? For example, let's say I have a string:
如何使用Python从字符串中删除重复的字符?例如,假设我有一个字符串:
foo = 'mppmt'
How can I make the string:
我该如何制作字符串:
foo = 'mpt'
NOTE: Order is not important
注意:订单并不重要
8 个解决方案
#1
58
If order does not matter, you can use
如果订单无关紧要,您可以使用
"".join(set(foo))
set()
will create a set of unique letters in the string, and "".join()
will join the letters back to a string in arbitrary order.
set()将在字符串中创建一组唯一字母,“”。join()将以任意顺序将字母连接回字符串。
If order does matter, you can use collections.OrderedDict
in Python 2.7:
如果顺序很重要,可以在Python 2.7中使用collections.OrderedDict:
from collections import OrderedDict
foo = "mppmt"
print "".join(OrderedDict.fromkeys(foo))
printing
mpt
#2
27
If order does matter, how about:
如果订单确实重要,那么:
>>> foo = 'mppmt'
>>> ''.join(sorted(set(foo), key=foo.index))
'mpt'
#3
3
If order is not the matter:
如果订单不是问题:
>>> foo='mppmt'
>>> ''.join(set(foo))
'pmt'
To keep the order:
保持订单:
>>> foo='mppmt'
>>> ''.join([j for i,j in enumerate(foo) if j not in foo[:i]])
'mpt'
#4
0
If order is important,
如果订单很重要,
seen = set()
result = []
for c in foo:
if c not in seen:
result.append(c)
seen.add(c)
result = ''.join(result)
Or to do it without sets:
或者没有套装:
result = []
for c in foo:
if c not in result:
result.append(c)
result = ''.join(result)
#5
0
As was mentioned "".join(set(foo)) and collections.OrderedDict will do. A added foo = foo.lower() in case the string has upper and lower case characters and you need to remove ALL duplicates no matter if they're upper or lower characters.
正如前面提到的那样。“。join(set(foo))和collections.OrderedDict会做。添加foo = foo.lower()以防字符串具有大写和小写字符,并且您需要删除所有重复项,无论它们是大写还是小写字符。
from collections import OrderedDict
foo = "EugeneEhGhsnaWW"
foo = foo.lower()
print "".join(OrderedDict.fromkeys(foo))
prints eugnhsaw
#6
0
#We can do it simply by using Regex libs of python.
#Check code and apply in your Program:
#Input= 'pppmm'
import re as reg
s='ppppmm'
pattern=reg.compile(r"(.)\1{1,}",reg.DOTALL)
string=pattern.sub(r"\1",s)
print(string)
#Output: pm
#7
0
Create a list in Python and also a set which doesn't allow any duplicates. Solution1 :
在Python中创建一个列表,也是一个不允许任何重复的集合。解决方案1:
def fix(string):
s = set()
list = []
for ch in string:
if ch not in s:
s.add(ch)
list.append(ch)
return ''.join(list)
string = "Protiijaayiiii"
print(fix(string))
Method 2 :
方法2:
s = "Protijayi"
aa = [ ch for i, ch in enumerate(s) if ch not in s[:i]]
print(''.join(aa))
#8
0
def dupe(str1):
s=set(str1)
return "".join(s)
str1='geeksforgeeks'
a=dupe(str1)
print(a)
works well if order is not important.
如果订单不重要,效果很好。
#1
58
If order does not matter, you can use
如果订单无关紧要,您可以使用
"".join(set(foo))
set()
will create a set of unique letters in the string, and "".join()
will join the letters back to a string in arbitrary order.
set()将在字符串中创建一组唯一字母,“”。join()将以任意顺序将字母连接回字符串。
If order does matter, you can use collections.OrderedDict
in Python 2.7:
如果顺序很重要,可以在Python 2.7中使用collections.OrderedDict:
from collections import OrderedDict
foo = "mppmt"
print "".join(OrderedDict.fromkeys(foo))
printing
mpt
#2
27
If order does matter, how about:
如果订单确实重要,那么:
>>> foo = 'mppmt'
>>> ''.join(sorted(set(foo), key=foo.index))
'mpt'
#3
3
If order is not the matter:
如果订单不是问题:
>>> foo='mppmt'
>>> ''.join(set(foo))
'pmt'
To keep the order:
保持订单:
>>> foo='mppmt'
>>> ''.join([j for i,j in enumerate(foo) if j not in foo[:i]])
'mpt'
#4
0
If order is important,
如果订单很重要,
seen = set()
result = []
for c in foo:
if c not in seen:
result.append(c)
seen.add(c)
result = ''.join(result)
Or to do it without sets:
或者没有套装:
result = []
for c in foo:
if c not in result:
result.append(c)
result = ''.join(result)
#5
0
As was mentioned "".join(set(foo)) and collections.OrderedDict will do. A added foo = foo.lower() in case the string has upper and lower case characters and you need to remove ALL duplicates no matter if they're upper or lower characters.
正如前面提到的那样。“。join(set(foo))和collections.OrderedDict会做。添加foo = foo.lower()以防字符串具有大写和小写字符,并且您需要删除所有重复项,无论它们是大写还是小写字符。
from collections import OrderedDict
foo = "EugeneEhGhsnaWW"
foo = foo.lower()
print "".join(OrderedDict.fromkeys(foo))
prints eugnhsaw
#6
0
#We can do it simply by using Regex libs of python.
#Check code and apply in your Program:
#Input= 'pppmm'
import re as reg
s='ppppmm'
pattern=reg.compile(r"(.)\1{1,}",reg.DOTALL)
string=pattern.sub(r"\1",s)
print(string)
#Output: pm
#7
0
Create a list in Python and also a set which doesn't allow any duplicates. Solution1 :
在Python中创建一个列表,也是一个不允许任何重复的集合。解决方案1:
def fix(string):
s = set()
list = []
for ch in string:
if ch not in s:
s.add(ch)
list.append(ch)
return ''.join(list)
string = "Protiijaayiiii"
print(fix(string))
Method 2 :
方法2:
s = "Protijayi"
aa = [ ch for i, ch in enumerate(s) if ch not in s[:i]]
print(''.join(aa))
#8
0
def dupe(str1):
s=set(str1)
return "".join(s)
str1='geeksforgeeks'
a=dupe(str1)
print(a)
works well if order is not important.
如果订单不重要,效果很好。