python去掉字符串中重复字符的方法

yipeiwu_com5年前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 基于wxpy库实现微信添加好友功能(简洁)

Python 基于wxpy库实现微信添加好友功能(简洁)

Github:https://github.com/Lyo-hub/wxpy_AddFriend 本程序为基于wxpy库实现的。 1.打开cmd导入一下库。 2.修改库文件中ad...

python下MySQLdb用法实例分析

本文实例讲述了python下MySQLdb用法。分享给大家供大家参考。具体分析如下: 下载安装MySQLdb ① linux版本 http://sourceforge.net/proje...

python正则表达式判断字符串是否是全部小写示例

实现代码 # -*- coding: cp936 -*- import re s1 = 'adkkdk' s2 = 'abc123efg' an = re.search('^[a...

python print输出延时,让其立刻输出的方法

一句print("ni hao"),很久看不见,怎么让python print能立刻输出呢。 因为python默认是写入stdout缓冲的,使用-u参数启动python,就会立刻输出了。...

Django 导出 Excel 代码的实例详解

Django 导出 Excel 代码的实例详解

这篇技术贴讲怎样在Django的框架下导出Excel, 最开始打算用ajax post data 过去,但是发现不行,所以改用了get的方式。如果只有一个id(pk)那用get的方式很简...