python去掉字符串中重复字符的方法

代码如下:

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.

if order does matter, you can use collections.ordereddict in python 2.7:

from collections import ordereddictfoo = “mppmt”print “”.join(ordereddict.fromkeys(foo))printing

mpt

Posted in 未分类

发表评论