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高级特性之闭包与装饰器实例详解

本文实例讲述了Python高级特性之闭包与装饰器。分享给大家供大家参考,具体如下: 闭包 1.函数参数: (1)函数名存放的是函数的地址 (2)函数名()存放的是函数内的代码 (3)...

python版百度语音识别功能

python版百度语音识别功能

本文实例为大家分享了python版百度语音识别功能的具体代码,供大家参考,具体内容如下 环境:使用的IDE是Pycharm 1.新建工程 2.配置百度语音识别环境 “File”——“Se...

python中logging库的使用总结

前言 最近因为工作的需要,在写一些python脚本,总是使用print来打印信息感觉很low,所以抽空研究了一下python的logging库,来优雅的来打印和记录日志,下面话不多说了,...

基于Python实现定时自动给微信好友发送天气预报

基于Python实现定时自动给微信好友发送天气预报

效果图 from wxpyimport * import requests from datetimeimport datetime import time from apsche...

pandas全表查询定位某个值所在行列的方法

如下所示: # create a dataframe with an integer feature and a categorical string feature demo_df...