zoukankan      html  css  js  c++  java
  • Python 数据排序和列表迭代和列表推导应用

    1.In-place sorting 原地排序

    data=[6,4,5,2,3,1]
    print ('before sort', data)
    data.sort()
    print ('after sort BIF:', data)
    
    =========== RESTART: C:/Users/eric/Documents/Python/kelly/sort.py ===========
    before sort [6, 4, 5, 2, 3, 1]
    after sort BIF: [1, 2, 3, 4, 5, 6]
    

    2. copied sorting 复制排序

    test=[6,4,5,2,3,1]
    print ('before sorted', test)
    test2=sorted(test)
    print ('after sorted BIF, test', test)
    print ('after sorted BIF, test2',test2)
    
    =========== RESTART: C:/Users/eric/Documents/Python/kelly/sort.py ===========
    before sorted [6, 4, 5, 2, 3, 1]
    after sorted BIF, test [6, 4, 5, 2, 3, 1]
    after sorted BIF, test2 [1, 2, 3, 4, 5, 6]
    

    3. use senitize func 列表迭代处理各个选手的列表数据,将清理过的值追加到适当新列表

    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(',')
    
    with open('julie.txt') as jue:  data=jue.readline()
    julie=data.strip().split(',')
    
    with open('mikey.txt') as miy:  data=miy.readline()
    mikey=data.strip().split(',')
    
    with open('sarah.txt') as sah:  data=sah.readline()
    sarah=data.strip().split(',')
    
    print ('before sort and clean data' ,james,julie,mikey,sarah)
    
    clean_james=[]
    clean_julie=[]
    clean_mikey=[]
    clean_sarah=[]
    
    for each_t in james:
        clean_james.append(sanitize(each_t))
    for each_t in julie:
        clean_julie.append(sanitize(each_t))
    for each_t in mikey:
        clean_mikey.append(sanitize(each_t))
    for each_t in sarah:
        clean_sarah.append(sanitize(each_t))
    
    print('after clean and sorted james is :',sorted(clean_james))
    print('after clean and sorted julie is :',sorted(clean_julie))
    print('after clean and sorted mikey is :',sorted(clean_mikey))
    print('after clean and sorted sarah is :',sorted(clean_sarah))
    
    =========== RESTART: C:UsersericDocumentsPythonkellykelly.py ===========
    before sort and clean data ['2-34', '3:21', '2.34', '2.45', '3.01', '2:01', '2:01', '3:10', '2-22'] ['2.59', '2.11', '2:11', '2:23', '3-10', '2-23', '3:10', '3.21', '3-21'] ['2:22', '3.01', '3:01', '3.02', '3:02', '3.02', '3:22', '2.49', '2:38'] ['2:58', '2.58', '2:39', '2-25', '2-55', '2:54', '2.18', '2:55', '2:55']
    after clean and sorted james is : ['2.01', '2.01', '2.22', '2.34', '2.34', '2.45', '3.01', '3.10', '3.21']
    after clean and sorted julie is : ['2.11', '2.11', '2.23', '2.23', '2.59', '3.10', '3.10', '3.21', '3.21']
    after clean and sorted mikey is : ['2.22', '2.38', '2.49', '3.01', '3.01', '3.02', '3.02', '3.02', '3.22']
    after clean and sorted sarah is : ['2.18', '2.25', '2.39', '2.54', '2.55', '2.55', '2.55', '2.58', '2.58']
    

    4.list comprehension 运用 “列表推导”减少代码,达到同样效果

    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(',')
    with open('julie.txt') as jue:  data=jue.readline()
    julie=data.strip().split(',')
    with open('mikey.txt') as miy:  data=miy.readline()
    mikey=data.strip().split(',')
    with open('sarah.txt') as sah:  data=sah.readline()
    sarah=data.strip().split(',')
    
    print ('before sort and clean data' ,james,julie,mikey,sarah)
    
    clean_james=[sanitize(each_t) for each_t in james]
    clean_julie=[sanitize(each_t) for each_t in julie]
    clean_mikey=[sanitize(each_t) for each_t in mikey]
    clean_sarah=[sanitize(each_t) for each_t in sarah]
    
    print('after clean and sorted james is :',sorted(clean_james))
    print('after clean and sorted julie is :',sorted(clean_julie))
    print('after clean and sorted mikey is :',sorted(clean_mikey))
    print('after clean and sorted sarah is :',sorted(clean_sarah))
    
    >>> 
    =========== RESTART: C:UsersericDocumentsPythonkellykelly.py ===========
    before sort and clean data ['2-34', '3:21', '2.34', '2.45', '3.01', '2:01', '2:01', '3:10', '2-22'] ['2.59', '2.11', '2:11', '2:23', '3-10', '2-23', '3:10', '3.21', '3-21'] ['2:22', '3.01', '3:01', '3.02', '3:02', '3.02', '3:22', '2.49', '2:38'] ['2:58', '2.58', '2:39', '2-25', '2-55', '2:54', '2.18', '2:55', '2:55']
    after clean and sorted james is : ['2.01', '2.01', '2.22', '2.34', '2.34', '2.45', '3.01', '3.10', '3.21']
    after clean and sorted julie is : ['2.11', '2.11', '2.23', '2.23', '2.59', '3.10', '3.10', '3.21', '3.21']
    after clean and sorted mikey is : ['2.22', '2.38', '2.49', '3.01', '3.01', '3.02', '3.02', '3.02', '3.22']
    after clean and sorted sarah is : ['2.18', '2.25', '2.39', '2.54', '2.55', '2.55', '2.55', '2.58', '2.58']
    
  • 相关阅读:
    poj3195 Generalized Matrioshkas(瞎搞题翻译)
    hdu2544最短路
    hdu2544最短路
    poj3195 Generalized Matrioshkas(栈)
    poj3195 Generalized Matrioshkas(栈)
    bzoj3171 [Tjoi2013]循环格
    bzoj3171 [Tjoi2013]循环格
    bzoj2245 [SDOI2011]工作安排
    bzoj2245 [SDOI2011]工作安排
    bzoj2668 [cqoi2012]交换棋子
  • 原文地址:https://www.cnblogs.com/oskb/p/4845942.html
Copyright © 2011-2022 走看看