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多线程并发让两个LED同时亮的方法

python多线程并发让两个LED同时亮的方法

在做毕业设计的过程中,想对多个传感器让他们同时并发执行。之前想到 light_red() light_blue() 分别在两个shell脚本中同时运行,但是这样太麻烦了。后来学到了Pyt...

python实现简单的计时器功能函数

本文实例讲述了python实现简单的计时器功能函数。分享给大家供大家参考。具体如下: 此函数通过python实现了一个简单的计时器动能: ''' Simple Timing Func...

判断python字典中key是否存在的两种方法

今天来说一下如何判断字典中是否存在某个key,一般有两种通用做法,下面为大家来分别讲解一下: 第一种方法:使用自带函数实现。 在python的字典的属性方法里面有一个has_key()方...

Python语言实现获取主机名根据端口杀死进程

推荐阅读:使用python检测主机存活端口及检查存活主机 下面给大家分享使用python语言实现获取主机名根据端口杀死进程代码。 ip=os.popen("ifconfig eth0...

python安装教程 Pycharm安装详细教程

python安装教程 Pycharm安装详细教程

python安装教程和Pycharm安装详细教程,分享给大家。 首先我们来安装python 1、首先进入网站下载:点击打开链接(或自己输入网址https://www.python.org...