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解决鸡兔同笼问题的方法

本文实例讲述了Python解决鸡兔同笼问题的方法,分享给大家供大家参考。具体分析如下: 问题描述 一个笼子里面关了鸡和兔子(鸡有 2 只脚,兔子有 4 只脚,没有例外)。已经知道了笼 子...

python中for用来遍历range函数的方法

python中for用来遍历range函数的方法

栗子:计算斐波那契数列(任一个数都是前两个数之和的数字序列) Python2.7实现代码如下: <strong><span style="font-size:14p...

Python3 文章标题关键字提取的例子

Python3 文章标题关键字提取的例子

思路: 1.读取所有文章标题; 2.用“结巴分词”的工具包进行文章标题的词语分割; 3.用“sklearn”的工具包计算Tf-idf(词频-逆文档率); 4.得到满足关键词权重阈值的词...

python中__call__方法示例分析

本文实例讲述了python中__call__方法的用法,分享给大家供大家参考。具体方法分析如下: Python中的__call__允许程序员创建可调用的对象(实例),默认情况下, __c...

解决pycharm界面不能显示中文的问题

主题的修改: File -> Settings , 弹出的窗口中: Appearance & Behavior -> Appearance , 可以修改“Theme”。 换成...