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

相关文章

对Python3 序列解包详解

Python 中有很多很实用的语法糖,这些语法糖可以帮助我们简化代码、更易理解等优点,接下里再看一个 Python3 中特别实用的语法序列解包(序列解包是 Python 3.0 之后才有...

python模块常用用法实例详解

1、time模块(※※※※) import time #导入时间模块 print(time.time()) #返回当前时间的时间戳,可用于计算程序运行时间 print(time.l...

python调用百度地图WEB服务API获取地点对应坐标值

本篇博客介绍如何使用Python调用百度地图WEB服务API获取地点对应坐标值,现有一系列结构化地址数据(如:北京市海淀区上地十街十号),目的是获取对应坐标值。 百度地图开发者平台路线规...

django框架基于queryset和双下划线的跨表查询操作详解

django框架基于queryset和双下划线的跨表查询操作详解

本文实例讲述了django框架基于queryset和双下划线的跨表查询操作。分享给大家供大家参考,具体如下: 前面篇随笔写的是基于对象的跨表查询:对象.objects.filter(。。...

使用Python实现租车计费系统的两种方法

要求: #出租车计费***********************************************************************************...