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对象转换为json的方法步骤

Python中内置了json库,用起来超级方便,json现在以成为开发的必备。 python对象到json字符串的转换规则: Python...

python 实现上传图片并预览的3种方法(推荐)

python 实现上传图片并预览的3种方法(推荐)

在常见的用户注册页面,需要用户在本地选择一张图片作为头像,并同时预览。 常见的思路有两种:一是将图片上传至服务器的临时文件夹中,并返回该图片的url,然后渲染在html页面;另一种思路是...

python中如何使用insert函数

这篇文章主要介绍了python中如何使用insert函数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 描述 insert() 函数用...

详解python中xlrd包的安装与处理Excel表格

一、安装xlrd 地址 下载后,使用 pip install .whl 安装即好。 查看帮助: >>> import xlrd >>> help...

Python3 修改默认环境的方法

Mac 环境中既有自带的 Python2.7 也有自己安装的 Python 3.5.1,默认想用 Python3 的环境 1. 添加 Python3 的环境变量 vi ~/.bash...