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读取系统文件夹内所有文件并统计数量的方法

大家先看一下Python os模块中的部分函数 python 路径相关的函数 os.listdir(dirname):列出dirname下的目录和文件 os.getcwd():获得当前...

Python中几种导入模块的方式总结

模块内部封装了很多实用的功能,有时在模块外部调用就需要将其导入。常见的方式有如下几种: 1 . import >>> import sys >>>...

python文件的md5加密方法

本文实例讲述了python文件的md5加密方法。分享给大家供大家参考,具体如下: 简单模式: from hashlib import md5 def md5_file(name):...

对python_discover方法遍历所有执行的用例详解

对python_discover方法遍历所有执行的用例详解

当我们写了一个单个py的测试文件时直接运行就ok了,但当我们有很多很多个这样的py时,难道要一个一个的点击来运行吗,当然不是。我们可以通过discover方法来找到所有的用例。 下面直接...

Python实现Mysql数据统计及numpy统计函数

Python实现Mysql数据统计的实例代码如下所示: import pymysql import xlwt excel=xlwt.Workbook(encoding='utf-8'...