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

相关文章

pandas.DataFrame的pivot()和unstack()实现行转列

pandas.DataFrame的pivot()和unstack()实现行转列

示例:有如下表需要进行行转列: 代码如下: # -*- coding:utf-8 -*- import pandas as pd import MySQLdb from wa...

Pandas时间序列重采样(resample)方法中closed、label的作用详解

Pandas提供了便捷的方式对时间序列进行重采样,根据时间粒度的变大或者变小分为降采样和升采样: 降采样:时间粒度变大。例如,原来是按天统计的数据,现在变成按周统计。降采样会涉及到...

Python判断文件和文件夹是否存在的方法

一、python判断文件和文件夹是否存在、创建文件夹 复制代码 代码如下: >>> import os >>> os.path.exists('d:...

如何基于python生成list的所有的子集

这篇文章主要介绍了如何基于python生成list的所有的子集,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 不使用递归且不引入标准库...

在Python中处理字符串之isdecimal()方法的使用

 isdecimal()方法检查字符串是否仅由十进制字符组成。此方法只存在于unicode对象。 注意:要定义一个字符串为Unicode,只需前缀分配'u'左引号。以下是示例。...