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

相关文章

python3模块smtplib实现发送邮件功能

python3模块smtplib实现发送邮件功能

本文实例为大家分享了python3 smtplib发送邮件的具体代码,供大家参考,具体内容如下 smtplib模块是smtp简单邮件传输协议客户端的实现,为了通用性,有时候发送邮件的时候...

Python中字典(dict)和列表(list)的排序方法实例

一、对列表(list)进行排序 推荐的排序方式是使用内建的sort()方法,速度最快而且属于稳定排序复制代码 代码如下:>>> a = [1,9,3,7,2,0,5]&...

python selenium 弹出框处理的实现

弹出框有两种:页面弹出框(可定位元素能操作)、Windows弹出框(不能直接定位) 一、页面弹出框 等待弹出框出现之后,定位弹出框,操作其中元素 如:  driver = webdr...

Python基于OpenCV库Adaboost实现人脸识别功能详解

Python基于OpenCV库Adaboost实现人脸识别功能详解

本文实例讲述了Python基于OpenCV库Adaboost实现人脸识别功能。分享给大家供大家参考,具体如下: 以前用Matlab写神经网络的面部眼镜识别算法,研究算法逻辑,采集大量训练...

Python tkinter模块中类继承的三种方式分析

本文实例讲述了Python tkinter模块中类继承的三种方式。分享给大家供大家参考,具体如下: tkinter class继承有三种方式。 提醒注意这几种继承的运行方式 一、继承 o...