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实现列表中最大最小值输出的示例

如下所示: def findMinAndMax(L): maxL = None minL = None if L: maxL = L[0] minL =...

Python全局变量用法实例分析

本文实例讲述了Python全局变量用法。分享给大家供大家参考,具体如下: 全局变量不符合参数传递的精神,所以,平时我很少使用,除非定义常量。今天有同事问一个关于全局变量的问题,才发现其中...

在Python中实现函数重载的示例代码

在Python中实现函数重载的示例代码

假设你有一个函数connect,它有一个参数address,这个参数可能是一个字符串,也可能是一个元组。例如: connect('123.45.32.18:8080') connec...

python random从集合中随机选择元素的方法

如下所示: list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] slice = random.sample(list, 5) #从list中随机获取5个元...

Python变量和数据类型详解

Python 变量类型       变量存储在内存中的值。这就意味着在创建变量时会在内存中开辟一个空间。   &nb...