【发布时间】:2013-05-15 08:30:49
【问题描述】:
在 Python 中,我想从字符串中删除重复的字母,而不是数字或空格。我想出了:
result = []
seen = set()
for char in string:
if char not in seen:
seen.add(char)
result.append(char)
return "".join(result)
但这使得:
>>> delete_duplicate_letters("13 men were wounded in an explosion yesterday around 3:00pm.")
13 menwroudiaxplsyt:0.
当我想要时:
>>> delete_duplicate_letters("13 men were wounded in an explosion yesterday around 3:00pm.")
13 men wr oud i a xpls yt 3:00.
我尝试使用letter 代替char、isalpha() 函数和if int 语句等,但我什么都做不了。
【问题讨论】:
标签: python python-2.7 duplicate-removal alphanumeric