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神奇的内置函数locals的实例讲解

摘要 本文我们介绍神奇的locals函数,包括动态创建变量和动态访问变量,以及一个应用场景。 相同属性不相邻问题 需求:有两个list,分别为list1和list2。list1中...

python使用pymysql实现操作mysql

pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同。但目前pymysql支持python3.x而后者不支持3.x版本。 适用环境 python版本 &...

Pipenv一键搭建python虚拟环境的方法

Pipenv一键搭建python虚拟环境的方法

由于python2和python3在部分语法上不兼容, 导致有人打趣道:"Python2和Python3是两门语言" 对于初学者而言, 如果同时安装了python2和python3, 那...

详解Python中open()函数指定文件打开方式的用法

文件打开方式 当我们用open()函数去打开文件的时候,有好几种打开的模式。 'r'->只读 'w'->只写,文件已存在则清空,不存在则创建。 'a'->追加,写到文件...

python opencv 读取图片 返回图片某像素点的b,g,r值的实现方法

如下所示: #coding=utf-8 #读取图片 返回图片某像素点的b,g,r值 import cv2 import numpy as np img=cv2.imread('....