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实现mqtt的发布和订阅

需要安装的python库 使用python编写程序进行测试MQTT的发布和订阅功能。首先要安装:pip install paho-mqtt 测试发布(pub) 我的MQTT部署在阿里云的...

解决Atom安装Hydrogen无法运行python3的问题

解决Atom安装Hydrogen无法运行python3的问题

Atom是一款功能强大的跨平台编辑器,插件化的解决方案为atom社区的繁荣奠定了基础。任何人都可以把自己做的组件贡献在github上,并能方便的安装到Atom上使用。 Jupyter N...

Python 隐藏输入密码时屏幕回显的实例

我们再登录账号密码的时候,账号可以回显在屏幕上,但是对于比较隐私的项目例如密码最好是不要再屏幕上回显。就像我们再终端登录linux服务器的时候,输入信息的时候只显示用户名,而不显示登录密...

通过数据库向Django模型添加字段的示例

首先借用书本(book)的这个数据模型: from django.db import models class Publisher(models.Model): name =...

总结Python编程中函数的使用要点

为何使用函数 最大化代码的重用和最小化代码冗余 流程的分解 编写函数 >>def语句 在Python中创建一个函数是通过def关键字进行的,def语句将创建一个函...