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实现读取文件最后n行的方法

本文实例讲述了Python实现读取文件最后n行的方法。分享给大家供大家参考,具体如下: # -*- coding:utf8-*- import os import time impo...

浅谈python中真正关闭socket的方法

close方法可以释放一个连接的资源,但是不是立即释放,如果想立即释放,那么在close之前使用shutdown方法 shut_rd() -------关闭接受消息通道 shut_wr(...

图文详解WinPE下安装Python

图文详解WinPE下安装Python

本文介绍了WinPE下安装Python的具体步骤,供大家参考,具体内容如下 一、下载Python Windows安装包,最新版本为3.3.0 下载地址:http://www.python...

python使用PyGame播放Midi和Mp3文件的方法

本文实例讲述了python使用PyGame播放Midi和Mp3文件的方法。分享给大家供大家参考。具体实现方法如下: ''' pg_midi_sound101.py play midi...

Python Unittest自动化单元测试框架详解

Python Unittest自动化单元测试框架详解

本文实例为大家分享了Python Unittest自动化单元测试框架的具体代码,供大家参考,具体内容如下 1、python 测试框架(本文只涉及 PyUnit) 参考地址 2、环境准备...