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 输入一个数n,求n个数求乘或求和的实例

求和 try: while True: n=input() s=1 for x in raw_input().split(): s=s+int(x) prin...

Python中的sort()方法使用基础教程

一、基本形式 sorted(iterable[, cmp[, key[, reverse]]]) iterable.sort(cmp[, key[, reverse]]) &nbs...

啥是佩奇?使用Python自动绘画小猪佩奇的代码实例

啥是佩奇?使用Python自动绘画小猪佩奇的代码实例

最近社会猪可是火遍了大江南北,不蹭下热度可对不起它。见过手画的佩奇,见过用代码画的吗? 没有?那就来看我大显身手。 用python的turtle库来画小猪佩奇。 有人问:turtle难不...

python如何在终端里面显示一张图片

python如何在终端里面显示一张图片

Linux终端里面可谓是奇妙无限,很多优秀的软件都诞生在终端里面。相较之下,Windows本身的理念和Linux就不一致,所以,你懂得。 下面,我们不妨先思考一下,如何在终端里面显示一...

Python操作SQLite简明教程

一、SQLite简介 SQLite是一个包含在C库中的轻量级数据库。它并不需要独立的维护进程,并且允许使用非标准变体(nonstandard variant)的SQL查询语句来访问数据库...