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操作 hbase 数据的方法

配置 thrift python使用的包 thrift 个人使用的python 编译器是pycharm community edition. 在工程中设置中,找到project inte...

python pandas库中DataFrame对行和列的操作实例讲解

用pandas中的DataFrame时选取行或列: import numpy as np import pandas as pd from pandas import Sereis,...

基于python3 类的属性、方法、封装、继承实例讲解

Python 类 Python中的类提供了面向对象编程的所有基本功能:类的继承机制允许多个基类,派生类可以覆盖基类中的任何方法,方法中可以调用基类中的同名方法。 对象可以包含任意数量和类...

python实现多层感知器MLP(基于双月数据集)

python实现多层感知器MLP(基于双月数据集)

本文实例为大家分享了python实现多层感知器MLP的具体代码,供大家参考,具体内容如下 1、加载必要的库,生成数据集 import math import random impor...

从运行效率与开发效率比较Python和C++

之前有人一直在说python怎么怎么好用,也有人说C++太难了,下面我做了一些笔记: 1、运行效率:C++ >> Python Python代码和C++最终都会变成CPU指令...