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 中 function(#) (X)格式 和 (#)在Python3.*中的注意事项

python 的语法定义和C++、matlab、java 还是很有区别的。 1. 括号与函数调用 def devided_3(x): return x/3. print(a)...

python使用fileinput模块实现逐行读取文件的方法

本文实例讲述了python使用fileinput模块实现逐行读取文件的方法。分享给大家供大家参考。具体实现方法如下: #-------------------------------...

Python使用Flask-SQLAlchemy连接数据库操作示例

本文实例讲述了Python使用Flask-SQLAlchemy连接数据库操作。分享给大家供大家参考,具体如下: 需要安装flask pip install flask 安装My...

Python中第三方库Requests库的高级用法详解

一、Requests库的安装 利用 pip 安装,如果你安装了pip包(一款Python包管理工具,不知道可以百度哟),或者集成环境,比如Python(x,y)或者anaconda的话...

pytorch的batch normalize使用详解

torch.nn.BatchNorm1d() 1、BatchNorm1d(num_features, eps = 1e-05, momentum=0.1, affine=True) 对于...