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之Character string(实例讲解)

1、python字符串 字符串是 Python 中最常用的数据类型。我们可以使用引号('或")来创建字符串,l Python不支持单字符类型,单字符也在Python也是作为一个字符串使用...

Django stark组件使用及原理详解

Django stark组件使用及原理详解

 stark组件是仿照django的admin模块开发的一套组件,它的作用是在网页上对注册的数据表进行增删改查操作 一、配置 1、创建stark应用,在settings.py...

np.random.seed() 的使用详解

np.random.seed() 的使用详解

在学习人工智能时,大量的使用了np.random.seed(),利用随机数种子,使得每次生成的随机数相同。 我们带着2个问题来进行下列实验 np.random.seed()是否一直...

Python程序设计入门(1)基本语法简介

Python是当今日趋流行的一种脚本语言,它比Java更简单,比php更强大,并且还适用于做桌面应用的开发,在ubuntu中,更加是必须的一种脚本引擎,所以有必要去学习一下,这里的文章只...

Python中的index()方法使用教程

 index()方法确定字符串str,如果起始索引beg和结束索引end在末尾给出了找到字符串或字符串的一个子串。这个方法与find()方法一样,只是如果没有找到子符趾会抛出一...