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机器学习理论与实战(六)支持向量机

上节基本完成了SVM的理论推倒,寻找最大化间隔的目标最终转换成求解拉格朗日乘子变量alpha的求解问题,求出了alpha即可求解出SVM的权重W,有了权重也就有了最大间隔距离,但是其实上...

把项目从Python2.x移植到Python3.x的经验总结

 经历移植jinja2到python3的痛苦之后,我把项目暂时放一放,因为我怕打破python3的兼容。我的做法是只用一个python2的代码库,然后在安装的时候用2to3工具...

对python条件表达式的四种实现方法小结

如下所示: a, b, c = 1, 2, 3   # 1.常规   if a>b:     c...

Linux中Python 环境软件包安装步骤

简介: 记录一下关于 Python 环境软件包的一些安装步骤 1、升级 Python 到 2.7.10( 默认 2.6.6 ) shell > yum -y install e...

django foreignkey外键使用的例子 相当于left join

django外键使用 一对一 因为django中处于安全和方便将数据库中的表封装成模型,所以很多sql原生的功能无法使用, 比如 left join,但是我们可以使用外键(foreign...