python集合如何去除重复数据_Python 迭代删除重复项,集合删除重复项
发布日期:2021-06-24 11:26:36 浏览次数:3 分类:技术文章

本文共 2266 字,大约阅读时间需要 7 分钟。

1. 迭代删除重复项:先排序列表项,然后通过新迭代(not in)去除重复项,分片打印

def sanitize(time_string):

if '-' in time_string:

splitter = '-'

elif ':' in time_string:

splitter = ':'

else:

return (time_string)

(mins, secs)=time_string.split(splitter)

return(mins + '.' + secs)

with open ('james.txt') as jas: data = jas.readline()

james=data.strip().split(',')

print('before sort and senitize, unique for james',james)

james=sorted ([sanitize(t) for t in james])

unique_james=[]

for each_t in james:

if each_t not in unique_james:

unique_james.append(each_t)

print('First 3 time for james',unique_james[0:3])

=========== RESTART: C:\Users\eric\Documents\Python\kelly\kelly.py ===========

before sort and senitize, unique for james ['2-34', '3:21', '2.34', '2.45', '3.01', '2:01', '2:01', '3:10', '2-22']

First 3 time for james ['2.01', '2.22', '2.34']

2. 集合删除重复项:先set创建集合去除重复项,然后进行排序,分片打印

def sanitize(time_string):

if '-' in time_string:

splitter = '-'

elif ':' in time_string:

splitter = ':'

else:

return (time_string)

(mins, secs)=time_string.split(splitter)

return(mins + '.' + secs)

with open ('james.txt') as jas: data = jas.readline()

james=data.strip().split(',')

print('before sort and senitize, unique for james',james)

james=sorted (set([sanitize(t) for t in james]))

print('First 3 time for james',james[0:3])

=========== RESTART: C:\Users\eric\Documents\Python\kelly\kelly.py ===========

before sort and senitize, unique for james ['2-34', '3:21', '2.34', '2.45', '3.01', '2:01', '2:01', '3:10', '2-22']

First 3 time for james ['2.01', '2.22', '2.34']

3.精简代码,创建一个小函数rmspace去除空白符,通过函数调用分片打印

def sanitize(time_string):

if '-' in time_string:

splitter = '-'

elif ':' in time_string:

splitter = ':'

else:

return (time_string)

(mins, secs)=time_string.split(splitter)

return(mins + '.' + secs)

def rmspace(file):

with open(file) as fo: data=fo.readline()

return data.strip().split(',')

james=rmspace('james.txt')

print('before sort and senitize, unique for james',james)

print('First 3 time for james',sorted(set([sanitize(t) for t in james]))[0:3])

=========== RESTART: C:\Users\eric\Documents\Python\kelly\kelly.py ===========

before sort and senitize, unique for james ['2-34', '3:21', '2.34', '2.45', '3.01', '2:01', '2:01', '3:10', '2-22']

First 3 time for james ['2.01', '2.22', '2.34']

转载地址:https://blog.csdn.net/weixin_32497403/article/details/114467360 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:iview 自定义时间选择器组件_Vue.js中使用iView日期选择器并设置开始时间结束时间校验功能...
下一篇:java动态代码_Java Agent入门学习之动态修改代码

发表评论

最新留言

不错!
[***.144.177.141]2024年04月13日 08时40分36秒