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

相关文章

基于Django模板中的数字自增(详解)

基于Django模板中的数字自增(详解)

Django框架的模板提供了{% for %} 标签来进行循环 例如对集合进行循环是比较简单的 {% for row in v1 %} <div>{{row.name}}...

对python中raw_input()和input()的用法详解

最近用到raw_input()和input()来实现即时输入,就顺便找了些资料来看,加上自己所用到的一些内容,整理如下: 1、raw_input() raw_input([promp...

计算pytorch标准化(Normalize)所需要数据集的均值和方差实例

pytorch做标准化利用transforms.Normalize(mean_vals, std_vals),其中常用数据集的均值方差有: if 'coco' in args.dat...

一篇文章弄懂Python中所有数组数据类型

前言 数组类型是各种编程语言中基本的数组结构了,本文来盘点下Python中各种“数组”类型的实现。 list tuple array.array str bytes...

python中redis的安装和使用

python下redis安装 用python操作redis数据库,先下载redis-py模块下载地址https://github.com/andymccurdy/redis-py she...