欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-30 20:31:10
复制代码 代码如下:if order does not matter, you can use "".join(set(foo))set() will create a...

复制代码 代码如下:

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 ordereddict
foo = "mppmt"
print "".join(ordereddict.fromkeys(foo))
printing

mpt