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

相关文章

numpy数组之存取文件的实现示例

将 numpy 数组存入文件,有多种文件类型可供选择,对应地就有不同的方法来读写。 下面我将介绍读写 numpy 的三类文件: txt 或者 csv 文件 npy 或者 npz...

详解numpy矩阵的创建与数据类型

详解numpy矩阵的创建与数据类型

Numpy是python常用的一个类库,在python的使用中及其常见,广泛用在矩阵的计算中,numpy对矩阵的操作与纯python比起来速度有极大的差距。 一、 构造矩阵 矩阵的构造...

python学生管理系统代码实现

本文实例为大家分享了python学生管理系统的具体代码,供大家参考,具体内容如下 类 class Student: stuID = "" name = "" sex =...

Python2和Python3.6环境解决共存问题

Linux下安装Python3.6和第三方库 /post/150478.htm 如果本机安装了python2,尽量不要管他,使用python3运行python脚本就好,因为可能有程序依...

numpy下的flatten()函数用法详解

flatten是numpy.ndarray.flatten的一个函数,其官方文档是这样描述的: ndarray.flatten(order='C') Return a copy of...