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判断完全平方数的方法

如下所示: # -*- coding: utf-8 -*- #简述:一个整数,它加上100和加上268后都是一个完全平方数 #提问:请问该数是多少? from math imp...

利用python对Excel中的特定数据提取并写入新表的方法

最近刚开始学python,正好实习工作中遇到对excel中的数据进行处理的问题,就想到利用python来解决,也恰好练手。 实际的问题是要从excel表中提取日期、邮件地址和时间,然后统...

Python Numpy:找到list中的np.nan值方法

Python Numpy:找到list中的np.nan值方法

这个问题源于在训练机器学习的一个模型时,使用训练数据时提示prepare的数据中存在np.nan 报错信息如下: ValueError: np.nan is an invalid...

PyTorch 解决Dataset和Dataloader遇到的问题

今天在使用PyTorch中Dataset遇到了一个问题。先看代码 class psDataset(Dataset): def __init__(self, x, y, trans...

利用Python获取赶集网招聘信息前篇

如何获取一个网站的相关信息,获取赶集网的招聘信息,本文为大家介绍利用python获取赶集网招聘信息的关键代码,供大家参考,具体内容如下 import re import urllib...