如何从字符串中删除重复的字符?

时间:2019-11-11 09:32:48

标签: python python-3.x list

如何从字符串中删除所有重复的字符?

例如:

Input:  string = 'Hello'
Output: 'Heo'

Removing duplicate characters from a string中的另一个问题,因为我不想打印出重复项,但是我想删除它们。

5 个答案:

答案 0 :(得分:6)

您可以使用generator表达式和join这样的

>>> x = 'Hello'
>>> ''.join(c for c in x if x.count(c) == 1)
'Heo'

答案 1 :(得分:4)

您可以从字符串中构造一个Counter,并从中检索只在其中出现一次的计数器中的元素:

from collections import Counter

c = Counter(string)
''.join([i for i in string if c[i]==1])
# 'Heo'

答案 2 :(得分:0)

a = 'Hello'


list_a = list(a)

output = []
for i in list_a:
    if list_a.count(i) == 1:
        output.append(i)

''.join(output)

答案 3 :(得分:0)

除了其他答案外,还可以使用filter

s = 'Hello'
result = ''.join(filter(lambda c: s.count(c) == 1, s))
# result - Heo

答案 4 :(得分:0)

如果将问题限制为仅包含连续连续字母的情况(如您的示例所示),则可以使用正则表达式:

import re
print(re.sub(r"(.)\1+", "", "hello"))     # result = heo
print(re.sub(r"(.)\1+", "", "helloo"))    # result = he
print(re.sub(r"(.)\1+", "", "hellooo"))   # result = he
print(re.sub(r"(.)\1+", "", "sports"))    # result = sports

如果您需要多次重新应用正则表达式,则值得事先对其进行编译:

prog = re.compile(r"(.)\1+")
print(prog.sub("", "hello"))

要限制在某些字符子集上搜索重复的字母,可以相应地调整正则表达式。

print(re.sub(r"(\S)\1+", "", "hello"))     # Search duplicated non-whitespace chars
print(re.sub(r"([a-z])\1+", "", "hello"))  # Search for duplicated lowercase letters

或者,使用列表理解的方法可能如下所示:

from itertools import groupby
dedup = lambda s: "".join([i for i, g in groupby(s) if len(list(g))==1])
print(dedup("hello"))     # result = heo
print(dedup("helloo"))    # result = he
print(dedup("hellooo"))   # result = he
print(dedup("sports"))    # result = sports

请注意,第一种使用正则表达式的方法在我的机器上比第二种快8-10倍。 (系统:python 3.6.7,MacBook Pro(2015年中))