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计算阶乘的几种方法

第一种:利用functools 工具处理 import functools result = (lambda k: functools.reduce(int.__mul__, ran...

Python yield 使用浅析

初学 Python 的开发者经常会发现很多 Python 函数中用到了 yield 关键字,然而,带有 yield 的函数执行流程却和普通函数不一样,yield 到底用来做什么,为什么要...

Python实现八皇后问题示例代码

八皇后问题描述 问题: 国际象棋棋盘是8 * 8的方格,每个方格里放一个棋子。皇后这种棋子可以攻击同一行或者同一列或者斜线(左上左下右上右下四个方向)上的棋子。在一个棋盘上如果要放八个...

举例详解Python中的split()函数的使用方法

函数:split() Python中有split()和os.path.split()两个函数,具体作用如下: split():拆分字符串。通过指定分隔符对字符串进行切片,并返回分割后的字...