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操作Word批量生成文章的方法

Python操作Word批量生成文章的方法

下面通过COM让Python与Word建立连接实现Python操作Word批量生成文章,具体介绍请看下文: 需要做一些会议记录。总共有多少呢?五个地点x7个月份x每月4篇=140篇。虽然...

Python实现希尔排序算法的原理与用法实例分析

Python实现希尔排序算法的原理与用法实例分析

本文实例讲述了Python实现希尔排序算法的原理与用法。分享给大家供大家参考,具体如下: 希尔排序(Shell Sort)是插入排序的一种。也称缩小增量排序,是直接插入排序算法的一种更高...

python 执行终端/控制台命令的例子

如下所示: import os os.system() os.popen().read().strip() #上面2种方法 是python 执行终端/控制台 命令的常见方法 #os...

python使用内存zipfile对象在内存中打包文件示例

复制代码 代码如下:import zipfileimport StringIO class InMemoryZip(object):    def __in...

Python用for循环实现九九乘法表

下面通过一段代码给大家介绍python 使用for 循环实现九九乘法表,具体代码如下所示: #for 循环实现99乘法表 for i in range (1,10): for j...