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函数式编程指南(二):从函数开始

2. 从函数开始 2.1. 定义一个函数 如下定义了一个求和函数: 复制代码 代码如下: def add(x, y):     return x + y...

Python实现计算文件MD5和SHA1的方法示例

本文实例讲述了Python实现计算文件MD5和SHA1的方法。分享给大家供大家参考,具体如下: 不多说,直接源码: #file md5 import sys; import hash...

python实现淘宝秒杀脚本

python实现淘宝秒杀脚本

本文实例为大家分享了python实现淘宝秒杀脚本的具体代码,供大家参考,具体内容如下 1.安装pycharm。网上教程很多。 2.安装 Selenium 库。 Selenium支持很多浏...

Python实现字符串匹配算法代码示例

字符串匹配存在的问题 Python中在一个长字符串中查找子串是否存在可以用两种方法:一是str的find()函数,find()函数只返回子串匹配到的起始位置,若没有,则返回-1;二是re...

python安装scipy的步骤解析

1、由于国外网站太慢,所以这里使用的是阿里的镜像 https://mirrors.aliyun.com/pypi/simple/ 2、去官网查看,官方给出的安装方法如下:【pip安装和...