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中dictionary items()系列函数的用法实例

本文实例讲述了Python中dictionary items()系列函数的用法,对Python程序设计有很好的参考借鉴价值。具体分析如下: 先来看一个示例: import html...

Python 初始化多维数组代码

    Python中初始化一个5 x 3每项为0的数组,最好方法是:multilist = [[0 for col i...

python网络编程之数据传输UDP实例分析

本文实例讲述了python网络编程之数据传输UDP实现方法。分享给大家供大家参考。具体分析如下: 一、问题: 你觉得网络上像msn,qq之类的工具在多台机器之间互相传输数据神秘吗?你也想...

Python算法之求n个节点不同二叉树个数

问题 创建一个二叉树 二叉树有限多个节点的集合,这个集合可能是: 空集 由一个根节点,和两棵互不相交的,分别称作左子树和右子树的二叉树组成 创建二叉树: 创建节点 再创建节...

pytorch 转换矩阵的维数位置方法

例如: preds = to_numpy(preds)#preds是[2985x16x2] preds = preds.transpose(2, 1, 0)#preds[2x16x2...