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用reduce和map把字符串转为数字的方法

python中reduce和map简介 map(func,seq1[,seq2...]) :将函数func作用于给定序列的每个元素,并用一个列表来提供返回值;如果func为None,fu...

python web基础之加载静态文件实例

在web运行中很重要的一个功能就是加载静态文件,在django中可能已经给我们设置好了,我们只要直接把模板文件 放在templates就好了,但是你知道在基础中,像图片是怎么加载以及找到...

用Python创建声明性迷你语言的教程

大多数程序员考虑编程时,他们都要设想用于编写应用程序的 命令式样式和技术。最受欢迎的通用编程语言(包括 Python 和其它面向对象的语言)在样式上绝大多数都是命令式的。另一方面,也有许...

python实现局域网内实时通信代码

使用场景,本地服务器一直在运算数据,实时发送这些数据给客户端,本地局域网内其他客户,可以实时连接服务器,获取服务器数据,互不影响。 python2服务端 #-*- coding:ut...

Python使用win32 COM实现Excel的写入与保存功能示例

Python使用win32 COM实现Excel的写入与保存功能示例

本文实例讲述了Python使用win32 COM实现Excel的写入与保存功能。分享给大家供大家参考,具体如下: 很久之前通过东拼西凑实现过使用Python通过win32 COM实现wo...