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 中文乱码问题深入分析

在本文中,以'哈'来解释作示例解释所有的问题,“哈”的各种编码如下: 1. UNICODE (UTF8-16),C854; 2. UTF-8,E59388; 3. GBK,B9FE。 一...

Python内置random模块生成随机数的方法

本文我们详细地介绍下两个模块关于生成随机序列的其他使用方法。 随机数参与的应用场景大家一定不会陌生,比如密码加盐时会在原密码上关联一串随机数,蒙特卡洛算法会通过随机数采样等等。Pytho...

Python内置模块logging用法实例分析

本文实例讲述了Python内置模块logging用法。分享给大家供大家参考,具体如下: 1、将日志直接输出到屏幕 import logging logging.debug('This...

python学生信息管理系统

本文实例为大家分享了python学生信息管理系统的具体代码,供大家参考,具体内容如下 #编译环境为python3 #学生信息管理系统包括基本的信息功能,能够实现学生信息的输入,...

python端口扫描系统实现方法

本文实例讲述了python端口扫描系统实现方法。分享给大家供大家参考。具体实现方法如下: 该程序的主要功能如下: 1. 从自有API接口获取所有的外网IP段; 2. 用Nmap 遍历扫描...