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

yipeiwu_com6年前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 OrderedDict
foo = "mppmt"
print "".join(OrderedDict.fromkeys(foo))
printing

mpt

相关文章

Python基础练习之用户登录实现代码分享

python版本为python3.5 1.要求 1)输入用户名密码 2)认证成功后显示欢迎信息 3)输错三次后锁定 2.需求分析 1)用户信息存储在文件中(login/config...

python实现简单ftp客户端的方法

本文实例讲述了python实现简单ftp客户端的方法。分享给大家供大家参考。具体实现方法如下: #!/usr/bin/python # -*- coding: utf-8 -*- i...

python pandas dataframe 按列或者按行合并的方法

concat 与其说是连接,更准确的说是拼接。就是把两个表直接合在一起。于是有一个突出的问题,是横向拼接还是纵向拼接,所以concat 函数的关键参数是axis 。 函数的具体参数是:...

Python考拉兹猜想输出序列代码实践

考拉兹猜想(英语:Collatz conjecture),是指对于每一个正整数,如果它是奇数,则对它乘3再加1,如果它是偶数,则对它除以2,如此循环,最终都能够得到1。(摘自Wiki)...

浅谈python中set使用

浅谈python中set使用 In [2]: a = set() # 常用操作1 In [3]: a Out[3]: set() In [4]: type(a) O...