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中如何实现将数据分成训练集与测试集的方法

接下来,直接给出大家响应的代码,并对每一行进行标注,希望能够帮到大家。 需要用到的是库是。numpy 、sklearn。 #导入相应的库(对数据库进行切分需要用到的库是sklearn...

简单实现python画圆功能

简单实现python画圆功能

本文实例为大家分享了python实现画圆功能的具体代码,供大家参考,具体内容如下 import numpy as np import matplotlib.pyplot as p...

Python适配器模式代码实现解析

Python适配器模式,代码,思考等 # -*- coding: utf-8 -*- # author:baoshan class Computer: def __init__(...

在Python的web框架中编写创建日志的程序的教程

在Python的web框架中编写创建日志的程序的教程

在Web开发中,后端代码写起来其实是相当容易的。 例如,我们编写一个REST API,用于创建一个Blog: @api @post('/api/blogs') def api_cre...

pyspark操作MongoDB的方法步骤

pyspark操作MongoDB的方法步骤

如何导入数据 数据可能有各种格式,虽然常见的是HDFS,但是因为在Python爬虫中数据库用的比较多的是MongoDB,所以这里会重点说说如何用spark导入MongoDB中的数据。...