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 不关闭控制台的实现方法

直接打开dos窗口,再执行python程序 在脚本的最后一行后面添加:raw_input()语句,这样直到按下回车键,窗口才关闭。 使用time模块的sleep函数,它有一个参数,传入数...

python实现折半查找和归并排序算法

python实现折半查找和归并排序算法

今天依旧是学算法,前几天在搞bbs项目,界面也很丑,评论功能好像也有BUG。现在不搞了,得学下算法和数据结构,笔试过不了,连面试的机会都没有…… 今天学了折半查找算法,折半查找是蛮简单的...

利用Python如何生成便签图片详解

利用Python如何生成便签图片详解

前言 最近有文字转图片的需求,但是不太想下载 APP,就使用 Python Pillow 实现了一个,效果如下: PIL 提供了 PIL.ImageDraw.ImageDraw.te...

Python3中的真除和Floor除法用法分析

本文实例讲述了Python3中的真除和Floor除法用法。分享给大家供大家参考,具体如下: 在Python3中,除法运算有两种,一种是真除,一种是Floor除法,这两者是有分别的,分别如...

在Python中os.fork()产生子进程的例子

例1 import os print 'Process (%s) start...' %os.getpid() pid = os.fork() if pid==0: print...