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 pickle类库介绍(对象序列化和反序列化)

一、pickle pickle模块用来实现python对象的序列化和反序列化。通常地pickle将python对象序列化为二进制流或文件。   python对象与文件之间的序列...

在python win系统下 打开TXT文件的实例

如下所示: fr=open("E:\Python\Test\datingTestSet.txt") arrayOLines = fr.readlines() open:打开文件 r...

基于python yield机制的异步操作同步化编程模型

本文总结下如何在编写python代码时对异步操作进行同步化模拟,从而提高代码的可读性和可扩展性。      游戏引擎一般都采用分布式框架,通过一定...

Pyhton中单行和多行注释的使用方法及规范

Pyhton中单行和多行注释的使用方法及规范

前言 注释可以起到一个备注的作用,团队合作的时候,个人编写的代码经常会被多人调用,为了让别人能更容易理解代码的通途,使用注释是非常有效的。 Python  注释符 一、py...

python base64库给用户名或密码加密的流程

给明文密码加密的流程: import base64 pwd_after_encrypt = base64.b64encode(b'this is a scret!') pwd_bef...