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计数 value_counts()的使用

pandas计数 value_counts()的使用

在pandas里面常用value_counts确认数据出现的频率。 1. Series 情况下: pandas 的 value_counts() 函数可以对Series里面的每个值进行计...

利用Python检测URL状态

需求:Python检测URL状态,并追加保存200的URL 代码一: #! /usr/bin/env python #coding=utf-8 import sys import...

Python的组合模式与责任链模式编程示例

组合模式 我们把Composite模式看成一个复杂的属性结构,其实基本有三种角色:树干(定义一些操作树叶leaf的操作),树枝(树干上有很多树枝)和树叶(树干想要具体操作的对象) ,Co...

Python3基础之基本运算符概述

Python3基础之基本运算符概述

本文所述为Python3的基本运算符,是学习Python必须掌握的,共享给大家参考一下。具体如下: 首先Python中的运算符大部分与C语言的类似,但也有很多不同的地方。这里就大概地罗列...

python 实现红包随机生成算法的简单实例

实例如下: </pre><pre name="code" class="python">#! /usr/bin/python # -*- coding: ut...