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

相关文章

Windows下python3安装tkinter的问题及解决方法

最近尝试写python GUI界面,决定先从tkinter开始。 但是遇到了无法安装。执行pip install tkinter没有用,报了如下错误: C:\Users\zhengji...

详解用python写一个抽奖程序

第一次使用python写程序,确实比C/C++之类方便许多。既然这个抽奖的数据不大,对效率要求并不高,所以采用python写,更加简洁、清晰、方便。 1.用到的模块 生成随机数的模...

浅谈numpy生成数组的零值问题

今天在用numpy写sinc函数时偶然发现在x=0时函数居然能取到1,觉得很不可思议,按理来说在x=0时函数无意义,研究了一下,发现竟然时numpy在生成数组时自动用一个很小的数代替了0...

python 实现单通道转3通道

下面有两种方法都可以: import numpy as np a=np.asarray([[10,20],[101,201]]) # a=a[:,:,np.newaxis] # p...

关于pip的安装,更新,卸载模块以及使用方法(详解)

在Python的学习过程中,肯定会遇到很多安装模块的地方,可以使用easy_install安装,但是easy_install相对于pip而言,最大的缺陷就是它所安装的模块是不能够卸载的,...