python去掉字符串中重复字符的方法

yipeiwu_com5年前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通过cv2读取多个USB摄像头

Python通过cv2读取多个USB摄像头

本文实例为大家分享了Python通过cv2读取多个USB摄像头的具体代码,供大家参考,具体内容如下 通过 cv2 可以轻易的拿到摄像头数据。 比如以下几步就能打开摄像头显示,并通过 q...

初步理解Python进程的信号通讯

信号的概念 信号(signal)--     进程之间通讯的方式,是一种软件中断。一个进程一旦接收到信号就会打断原来的程序执行流程来处理信号。 几...

python经典趣味24点游戏程序设计

python经典趣味24点游戏程序设计

一、游戏玩法介绍: 24点游戏是儿时玩的主要益智类游戏之一,玩法为:从一副扑克中抽取4张牌,对4张牌使用加减乘除中的任何方法,使计算结果为24。例如,2,3,4,6,通过( ( ( 4...

Python安装Imaging报错:The _imaging C module is not installed问题解决方法

今天写Python程序上传图片需要用到PIL库,于是到http://www.pythonware.com/products/pil/#pil117下载了一个1.1.7版本的,我用的是Ce...

在pytorch中查看可训练参数的例子

pytorch中我们有时候可能需要设定某些变量是参与训练的,这时候就需要查看哪些是可训练参数,以确定这些设置是成功的。 pytorch中model.parameters()函数定义如下:...