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基础教程之浅拷贝和深拷贝实例详解            网上关于Pytho...

python tkinter canvas 显示图片的示例

先来看一下该方法的说明 create_image(position, **options) [#] Draws an image on the canvas. position I...

解决python Markdown模块乱码的问题

解决python Markdown模块乱码的问题

有个需求需要把markdown转成html模块,查询了一下刚好有这个模块 安装 pip install amrkdown 安装完成直接转换并保存为html时,发现出现中文乱码的情况 用...

python网络编程之TCP通信实例和socketserver框架使用例子

python网络编程之TCP通信实例和socketserver框架使用例子

1.TCP是一种面向连接的可靠地协议,在一方发送数据之前,必须在双方之间建立一个连接,建立的过程需要经过三次握手,通信完成后要拆除连接,需要经过四次握手,这是由TCP的半关闭造成的,一方...

利用Python循环(包括while&for)各种打印九九乘法表的实例

利用Python循环(包括while&for)各种打印九九乘法表的实例

一.for循环打印九九乘法表 #注意:由于缩进在浏览器不好控制,请大家见谅,后续会有图片传入。 1.1 左下角   for i in range(1,10):     for j...