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常见工厂函数用法。分享给大家供大家参考,具体如下: 工厂函数:能够产生类实例的内建函数。  工厂函数是指这些内建函数都是类对象, 当调用它们时,实际上...

Atom Python 配置Python3 解释器的方法

环境 Mac Python3.6.4 Atom 背景 Atom 执行Python Code 使用Script Package,执行快捷键cmd + i。 但是默认是执行Mac 系统的2....

python实现向微信用户发送每日一句 python实现微信聊天机器人

分享几个Python针对微信的小工具,供大家参考,具体内容如下 用Python实现向微信用户发送每日一句 # -*- coding:utf-8 -*- from __future__...

Python入门_浅谈数据结构的4种基本类型

数据结构:通俗点说,就是储存大量数据的容器。这里主要介绍Python的4种基本数据结构:列表、字典、元组、集合。 格式如下: 列表:list = [val1,val2,val3,val4...

Flask框架Flask-Login用法分析

本文实例讲述了Flask框架Flask-Login用法。分享给大家供大家参考,具体如下: Flask-Login插件中带了6种信号,可以基于其中的信号做一些额外工作,比如user_log...