Python 去除字符中的重复字符 python过滤重复字符函数
1,python过滤重复字符代码一:
yizuotu.net= 'abc123456ab2s'
r = ''.join(x for i, x in enumerate(yizuotu.net)
if string.index(x) == i)
print stringprint r
输出如下:
abc123456s
输出去重排序后的结果字符串。
2,python过滤重复字符串二:
s="ad2f3adjfeainzzzv"
m=[]
for i in s:
if i not in m:
m.append(i)
m.sort(key=str,reverse=False)
print(''.join(m))
输出样例:23adefijnvz
3,Python字符串去重,去符号!经典
def zzzyk.com(string='', separators=','):
rst = [string] for sep in separators:
tmp = [] for r in rst:
tmp.extend(map(lambda x: x.strip(), r.split(sep)))
rst = tmp
list_tmp = []
[list_tmp.append(data) for data in rst if data != '']
return reduce(lambda x, y: (y in x) and x or (x + [y]), ([[], ] + list_tmp) )
print zzzyk.com('la,la,1,2,1+as%*ba', ',+*%')
Python字符串去重,去符号!经典
# coding=utf-8
#learning at jeapedu in 2013/10/26
#移除给定字符串中重复字符,参数s是字符串
def removeDuplicate(s):
s = list(s)
s.sort() #对给定字符串排序,不排序可能移除不完整
for i in s:
while s.count(i) > 1:
s.remove(i)
s = "".join(s) #把列表转换成字符串,不能用str(s)
return s
s1 = 'charming'
s1 = removeDuplicate(s1)
print s1
Python2.7.3移除字符串中重复字符,移除重复字符很简单,这里是最笨,也是最简单的一种。问题关键是理解排序的意义