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中hasattr()、getattr()、setattr()函数的使用

python中hasattr()、getattr()、setattr()函数的使用

 引言:   在阅读源码时,有很多简写的形式,其中一个比较常用的就是getattr()用来调用一个类中的变量或者方法,相关联的hasattr()、getattr()、setat...

说一说Python logging

说一说Python logging

最近有个需求是把以前字符串输出的log 改为json 格式,看了别人的例子,还是有些比较茫然,索性就把logging 整个翻了一边,做点小总结. 初看log 在程序中, log 的用处写...

python实现BP神经网络回归预测模型

python实现BP神经网络回归预测模型

神经网络模型一般用来做分类,回归预测模型不常见,本文基于一个用来分类的BP神经网络,对它进行修改,实现了一个回归模型,用来做室内定位。模型主要变化是去掉了第三层的非线性转换,或者说把非线...

如何高效使用Python字典的方法详解

前言 众所周知字典(dict)对象是 Python 最常用的数据结构,社区曾有人开玩笑地说:"Python企图用字典装载整个世界",字典在Python中的重要性不言而喻,这里整理了几个关...

python执行等待程序直到第二天零点的方法

本文实例讲述了python执行等待程序直到第二天零点的方法。分享给大家供大家参考。具体分析如下: 如果需要通过python每天凌晨定时执行执行程序,可以使用下面的代码进行等待操作,无论什...