Try this:
result = ""
for char in string:
if not (char.isalpha() and char in result):
result += char
Вопрос
In Python, I would like to remove duplicate letters from a string, but not numbers or spaces. I came up with:
result = []
seen = set()
for char in string:
if char not in seen:
seen.add(char)
result.append(char)
return "".join(result)
But that makes:
>>> delete_duplicate_letters("13 men were wounded in an explosion yesterday around 3:00pm.")
13 menwroudiaxplsyt:0.
When I want:
>>> delete_duplicate_letters("13 men were wounded in an explosion yesterday around 3:00pm.")
13 men wr oud i a xpls yt 3:00.
I've tried to use letter
instead of char
, the isalpha()
function and if int
statements etc but I couldn't get anything to work.
Решение 2
Try this:
result = ""
for char in string:
if not (char.isalpha() and char in result):
result += char
Другие советы
>>> from string import digits, whitespace
>>> from collections import OrderedDict
>>> s = set(whitespace + digits)
>>> ''.join(OrderedDict((object() if c in s else c, c) for c in text).values())
'12 men wr oud i a xpls yt 3:00.'
object()
here is just used to make sure the keys for characters you want to leave are always unique since object()
creates a different object each time. The other characters are used as the keys themselves so duplicates are filtered.
use str.isspace
and str.isdigit
:
strs = "13 men were wounded in an explosion yesterday around 3:00pm."
result = []
seen = set()
for char in strs:
if char not in seen:
if not (char.isspace() or char.isdigit()):
seen.add(char)
result.append(char)
print "".join(result)
output:
13 men wr oud i a xpls yt 3:00.
Seems like you're almost there. You can just add some checks in the loop:
result = []
seen = set()
for char in string:
if char.isdigit() or char.isspace():
result.append(char)
elif char not in seen:
seen.add(char)
result.append(char)
return "".join(result)