博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python集合如何去除重复数据_Python 迭代删除重复项,集合删除重复项
阅读量:1531 次
发布时间:2019-04-21

本文共 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']

转载地址:http://jkudy.baihongyu.com/

你可能感兴趣的文章
给采购凭证分配合作伙伴方案
查看>>
【常识】高尔夫计分规则
查看>>
高尔夫球专业术语
查看>>
SAP and ABAP Memory总结
查看>>
程序间数据共享与传递(1):EXPORT/IMPORT、SAP/ABAP Memory
查看>>
程序间数据共享与传递(2):EXPORT/IMPORT、SAP/ABAP Memory
查看>>
程序间数据共享与传递(3):EXPORT/IMPORT、SAP/ABAP Memory
查看>>
ABAP如何在调试查看EXPORT/IMPORT 内存数据
查看>>
LTRIM、RTRIM和TRIM在ORACLE中的用法:
查看>>
oracle常用的时间格式转换
查看>>
使用优化器提示(Optimizer Hints)
查看>>
销售抬头文本配置方法
查看>>
销售文本程序的分配
查看>>
日期和时间函数
查看>>
后真相时代(post-truth)出现的原因及影响
查看>>
[SAP ABAP开发技术总结]动态语句、动态程序
查看>>
ABAP高效率批量修改内表的数据
查看>>
oracle11g-expdp-impdp步骤
查看>>
定义国际贸易术语(Incoterms)
查看>>
定义交货输出条件类型(Output Types)
查看>>