Python 迭代删除重复项,集合删除重复项
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']
Python 迭代删除重复项,集合删除重复项的更多相关文章
- 【python】Leetcode每日一题-删除有序数组中的重复项
[python]Leetcode每日一题-删除有序数组中的重复项 [题目描述] 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现一次 ,返回删除后数组的新长度. 不要 ...
- 【python】Leetcode每日一题-删除有序数组中的重复项2
[python]Leetcode每日一题-删除有序数组中的重复项2 [题目描述] 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现两次 ,返回删除后数组的新长度. 不 ...
- SQL根据B表内容修改A表内容,查询表中重复记录,删除掉重复项只保留一条
以下sql是a,b两张表通过关联条件id修改a表值,如果b表有重复数据记录,选第一条更新,红色条件为附加限制条件,具体视情况而定: UPDATE a SETname = b.fname,pwd = b ...
- Leetcode_删除排序数组中的重复项
Leetcode 删除排序数组中的重复项 题目: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用 额外的数组空间,你必须在原地修改输入数 ...
- leetcode-26.删除重复数组中的重复项
leetcode-26.删除重复数组中的重复项 题意 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数 ...
- LeetCode 80. 删除排序数组中的重复项 II
LeetCode 80. 删除排序数组中的重复项 II
- leecode刷题(1)-- 删除排序数组中的重复项
删除排序数组中的重复项 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度.不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的 ...
- Oracle里删除重复记录,保留一项
我们在使用数据库的时候,有时数据会有所重复,当我们只需要一项数据时,不需要显示重复的记录时 如下就有SQL代码: --查找表中多余的重复记录,重复记录是根据单个字段来判断 select * from ...
- LeetCode:删除排序数组中的重复项||【80】
LeetCode:删除排序数组中的重复项||[80] 题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原 ...
随机推荐
- subprocess module
subprocess 主要用于执行外部命令和程序, 极大的增强了Python的功能. 比如你要用bowtie, 你可以在python中调用这个程序. 运行python时,我们都是在创建并运行一个进程, ...
- 无密码通过ssh执行rsync
默认情况下,在执行rsync命令时通常需要我们输入密码.但有时我们并不希望如此,那么如何实现无密码执行rsync呢? 1. 测试通过ssh可以执行rsync(需要密码) 执行rsync,确保你帐户的密 ...
- 114. Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- 关于Ajax知识点小节
URL:统一资源定位符 网络的七层协议:网卡 驱动 网络层(ip) 传输层(tcp udp) 会话层( ) 应用层(http.) restful表征状态转移(一种表征架构) CURD 增删改查 ...
- 放课后的约定 by:S_H_Y
题目背景 十年.十年前.天空的蔚蓝,云彩的舒软.我,怎么会认识你.洁白的,洁白的,十年前.我无法忘却.终究是小时候的约定.记忆被困在冻土里,有时我不认识那天的我.难道一切,都锁在心里面了吗? 题目描述 ...
- (转) TensorFlow深度学习,一篇文章就够了
TensorFlow深度学习,一篇文章就够了 2016/09/22 · IT技术 · TensorFlow, 深度学习 分享到:6 原文出处: 我爱计算机 (@tobe迪豪 ) 作者: 陈迪 ...
- libGraphicsMagickWand.so: cannot open shared object file: No such file or directory stack traceback:
参考博文:http://www.linuxidc.com/Linux/2016-07/133213.htm ==>> Check Passed, the num of bbox and f ...
- javascript零散要点收集
1.this永远指向函数对象的所有者 2.ECMA-262 把对象(object)定义为“属性的无序集合,每个属性存放一个原始值.对象或函数”.严格来说,这意味着对象是无特定顺序的值的数组. 3.pr ...
- weblogic管理2 - 创建并启动一个managed server
创建一个managed server. 1. 进入网页console管理页面,如:http://10.100.25.14:7001/console , 先点击->服务器 (红色标记框) ...
- Concurrent inserts on MyISAM and the binary log
Recently I had an interesting surprise with concurrent inserts into a MyISAM table. The inserts were ...