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函数中参数是传递值还是引用详解

Python函数中参数是传递值还是引用详解

在 C/C++ 中,传值和传引用是函数参数传递的两种方式,在Python中参数是如何传递的?回答这个问题前,不如先来看两段代码。 代码段1: def foo(arg): arg =...

详解python如何调用C/C++底层库与互相传值

前言 开发环境: Centos 7 + Python 3.5.1 + Qt Creator(只是使用Qt Creator编译而已,并没有使用QT的任何库) Python调用C/C++...

python中有关时间日期格式转换问题

python中有关时间日期格式转换问题

每次遇到pandas的dataframe某列日期格式问题总会哉坑,下面记录一下常用时间日期函数.... 1、字符串转化为日期 str—>date import datetime...

Tensorflow 自定义loss的情况下初始化部分变量方式

一般情况下,tensorflow里面变量初始化过程为: #variables ........... #..................... init = tf....

Python for循环与range函数的使用详解

for 循环 For … in 语句是另一种循环语句,其特点是会在一系列对象上进行迭代(Iterates),即它会遍历序列中的每一个项目 注意: 1、else 部分是可选的。当循环中包含...