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

相关文章

在dataframe两列日期相减并且得到具体的月数实例

如下所示: df = pd.DataFrame(np.random.randn(6,4), index=pd.date_range('20130101', periods=6), c...

python f-string式格式化听语音流程讲解

python f-string式格式化听语音流程讲解

f-string 格式化的字符串,是字符串格式化的一种,而且是最新的一种。这里收集的是它的一些基本用法。 没有限定宽度的写法:f"xxxx{ 替换字段 }xxx";  可以有多...

Pytorch DataLoader 变长数据处理方式

关于Pytorch中怎么自定义Dataset数据集类、怎样使用DataLoader迭代加载数据,这篇官方文档已经说得很清楚了,这里就不在赘述。 现在的问题:有的时候,特别对于NLP任务...

在Mac上删除自己安装的Python方法

推荐使用 Homebrew 来安装第三方工具。自己安装的python散落在电脑各处,删除起来比较麻烦。今天在此记录一下删除的过程(本人以Python3.6为例)。 删除Python 3....

详解使用Python下载文件的几种方法

在使用Python进行数据抓取的时候,有时候需要保持文件或图片等,在Python中可以有多种方式实现。今天就一起来学习下。 urllib.request 主要使用的是urlretrie...