zoukankan      html  css  js  c++  java
  • 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:UsersericDocumentsPythonkellykelly.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:UsersericDocumentsPythonkellykelly.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:UsersericDocumentsPythonkellykelly.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']
    
  • 相关阅读:
    维特比(Viterbi)算法解最优状态序列
    c#重要知识点复习1---程序流程控制
    学C# Hook原理及EasyHook简易教程
    EmguCV 绘画图形
    EmguCV创建/保存图片
    EmguCV中图像类型进行转换
    basler 相机拍照简单类综合Emgu.CV---得到图档--原创
    RotatedRect 类的用法
    EmguCv“线段” 结构类型学习
    aforge 学习-基本图像处理要用的类库
  • 原文地址:https://www.cnblogs.com/oskb/p/4846622.html
Copyright © 2011-2022 走看看