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 ftp 按目录结构上传下载的实现代码

具体代码如下所示: #!/usr/bin/python # coding=utf-8 from ftplib import FTP import time import os def...

浅谈Python 集合(set)类型的操作——并交差

阅读目录 •介绍 •基本操作 •函数操作 介绍 python的set是一个无序不重复元素集,基本功能包括关系测试和消除重复元素. 集合对象还支持并、交...

Windows下用py2exe将Python程序打包成exe程序的教程

py2exe在sourceforge 的下载只支持到2.7。 针对python3.0+的版本,需要自己编译。 1.下载源码 svn checkout svn://svn.code.sf....

Python使用logging模块实现打印log到指定文件的方法

Python使用logging模块实现打印log到指定文件的方法

本文实例讲述了Python使用logging模块实现打印log到指定文件的方法。分享给大家供大家参考,具体如下: 可能我们经常会使用print来输出信息到窗口,但当我们有很多个py文件需...

Python算法应用实战之队列详解

Python算法应用实战之队列详解

队列(queue) 队列是先进先出(FIFO, First-In-First-Out)的线性表,在具体应用中通常用链表或者数组来实现,队列只允许在后端(称为rear)进行插入操作,在前端...