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创建只读属性对象的方法(ReadOnlyObject)

复制代码 代码如下:def ReadOnlyObject(**args):    dictBI = {}    args_n...

OpenCV+Python识别车牌和字符分割的实现

OpenCV+Python识别车牌和字符分割的实现

本篇文章主要基于python语言和OpenCV库(cv2)进行车牌区域识别和字符分割,开篇之前针对在python中安装opencv的环境这里不做介绍,可以自行安装配置! 车牌号检测需要大...

PyTorch中topk函数的用法详解

PyTorch中topk函数的用法详解

听名字就知道这个函数是用来求tensor中某个dim的前k大或者前k小的值以及对应的index。 用法 torch.topk(input, k, dim=None, largest=...

在Python的while循环中使用else以及循环嵌套的用法

循环使用 else 语句 在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 bre...

解决Python的str强转int时遇到的问题

数字字符串前后有空格没事: >>> print(int(" 3 ")) 3 但是下面这种带小数点的情况是不可取的: >>> print(in...