python去掉字符串中重复字符的方法

yipeiwu_com5年前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

相关文章

matplotlib savefig 保存图片大小的实例

在用matplotlib画图时,如果图例比较大,画在图中就会挡着线条,这时可以用以下语句把图例画到图外面: plt.legend(bbox_to_anchor=(1.01, 1),...

处理Selenium3+python3定位鼠标悬停才显示的元素

先给大家介绍下Selenium3+python3--如何定位鼠标悬停才显示的元素 定位鼠标悬停才显示的元素,要引入新模块 # coding:utf-8 from selenium...

用python实现k近邻算法的示例代码

K近邻算法(或简称kNN)是易于理解和实现的算法,而且是你解决问题的强大工具。 什么是kNN kNN算法的模型就是整个训练数据集。当需要对一个未知数据实例进行预测时,kNN算法会在训...

Python中的类与类型示例详解

Python中的类与类型示例详解

1.经典类与新式类 在了解Python的类与类型前,需要对Python的经典类(classic classes)与新式类(new-style classes)有个简单的概念。 在Pyth...

python中实现字符串翻转的方法

具体代码如下所示: #字符串反转 def reverse (s): rt = '' for i in range(len(s)-1,-1,-1): rt += s[i...