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 Tkinter简单布局实例教程

Python Tkinter简单布局实例教程

本文实例展示了Python Tkinter实现简单布局的方法,示例中备有较为详尽的注释,便于读者理解。分享给大家供大家参考之用。具体如下: # -*- coding: utf-8 -...

pandas实现选取特定索引的行

如下所示: >>> import numpy as np >>> import pandas as pd >>> index=n...

pytorch:torch.mm()和torch.matmul()的使用

如下所示: torch.mm(mat1, mat2, out=None) → Tensor torch.matmul(mat1, mat2, out=None) → Tensor...

用python一行代码得到数组中某个元素的个数方法

想法由来 今天写代码过程中遇到一个需求,计算一个list中数值为1的元素的个数,其中这个list的元素数值不是为0就是为1。 一开始想到的是写个方法来计算: # 返回一个0,1数组中...

Python使用arrow库优雅地处理时间数据详解

前言 大家应该都知道在很多时候我们不得不和时间打交道,但在Python标准库中处理时间的模块其实设计的不是很友好,为什么我会这么说?因为我相信大部分人几乎每次在处理时间数据时一而再,再...