python學習筆記之sorted、sort、range、reverse函數

目錄

順序遍歷

逆向遍歷

實際排序問題


介紹

內置函數:mapreduce; filter;lambda 表達式

sortedsortrangereverse函數


內置函數

map:輸入兩個參數,第一個是函數,第二個是序列;返回函數fun作用於序列每個元素的組成的列表;
API解釋如下:

map(function, sequence[, sequence, ...]) -> list
    Return a list of the results of applying the function to the items of
    the argument sequence(s),For example, map(lambda x: x^2, [1, 2, 3, 4, 5]) ->[1, 4, 9, 16, 25]

reduce:輸入兩個參數,第一個是函數,第二個是序列;返回函數累計逐步作用於序列元素的最終結果;
API解釋如下:

reduce(function, sequence[, initial]) -> value
    
    Apply a function of two arguments cumulatively to the items of a sequence,
    from left to right, so as to reduce the sequence to a single value.
    For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
    ((((1+2)+3)+4)+5)
.  If initial is present, it is placed before the items
    of the sequence in the calculation, and serves as a default when the
    sequence is empty.

filter:輸入兩個參數,第一個是函數,第二個是序列;根據函數作用於序列中每個元素的中間結果(False或者True),決定丟棄或者保留該元素;

    filter(function or None, sequence) -> list, tuple, or string
    
    Return those items of sequence for which function(item) is true.  If
    function is None, return the items that are true.  If sequence is a tuple
    or string, return the same type, else return a list.


順序遍歷

說到順序遍歷,離不開迭代函數,range(list)enumerate(list);前者僅順序返回元素,後者不僅返回元素且返回元素對應索引

python的sorted函數和sort均可以對輸入序列排序;其區別在於是否保留原始序列;

sorted保留原始序列,且提供關鍵字key來指定特定的屬性字段排序,將排序後的結果保存爲新的列表;

#itervalues():返回字典value值的迭代器
#items():是可以將字典中的所有項,以列表方式返回,無序的狀態。
#iteritems():與items方法相比作用大致相同,只是它的返回值不是列表,而是一個迭代器。

#example1

>> dict1 = {"a":1, "c":4, "b":5} 

>>print sorted(dict1) #默認以key的大小進行排序,所以返回key組成的列表 # ['a', 'b', 'c']

>>print sorted(dict1_.iteritems()) # [('a',1), ('b',5), ('c',4)] #返回(key,value)形式的元組列表

>>print sorted(dict1, key = lambda x:dict1[x]) #以value的大小進行排序,# ['a', 'c', 'b']

#example2

>> dict_ = {"a":[1,4], "c":[6,2], "d":[4,3],"b":[5,6]} 

>>ret=sorted(dict_.itervalues(),key=lambda x:x[0])#返回字典的value,且以value第1列大小進行排序
>>print (ret) # [[1, 4], [4, 3], [5, 6], [6, 2]]

>>ret1 = sorted(dict_.itervalues(),key = lambda x:x[1]) #返回以value的第2列大小進行排序的列表
>> print ret1 # [[6, 2], [4, 3], [1, 4], [5, 6]]

>> print sorted(dict_.iteritems(),key = lambda x:x[1][0])#返回以value第1列大小排序的(key-value)元組列表
#[('a', [1, 4]), ('d', [4, 3]), ('b', [5, 6]), ('c', [6, 2])]

>> print sorted(dict_.iteritems(),key = lambda x:x[1][1]) 
#[('c', [6, 2]), ('d', [4, 3]), ('a', [1, 4]), ('b', [5, 6])]

sort自身排序,覆蓋原始序列;如,>>exp_list = [1,6,3,4,2] >> exp_list.sort(reverse=True)>>print(exp_list)


逆向遍歷

這裏提供幾種方式:

>>range(10)[::-1] #可以作爲新的對象返回
[9,8,7,6,5,4,3,2,1,0]
>> a=range(10)
>> a.reverse() #reverse方法並不能以新的變量或者對象返回,僅改變原始對象的順序
>>print(a)
[9,8,7,6,5,4,3,2,1,0]
>>reversed(range(10)) #返回的是一個迭代器對象,並不是具體的變量,需要for循環來遍歷加載
<listreverseiterator at 0x37f6241>

實際排序問題

問題描述:txt文件中有兩列字段,第一列是str型的人臉圖片絕對路徑;第二列是對應人臉的質量評分,根據評分,完成對人臉圖片的排序,對應關係不變。

思想:借鑑hash思想,進行排序;

# -*- coding: utf-8 -*-

import os
import numpy as np
import time 
from collections import OrderedDict

#divide the data into three subsets according to score of each image

def sort_dict(file_name ,dest_dir):
    bagan = time.time()
    if not os.path.exists(dest_dir):
        os.mkdir(dest_dir)
    
    #build the dict of (img_path, score) pairs
    src_dict = OrderedDict()
    
    with open(file_name,'r') as f:
        #len(f.readlines())
        for item in f.readlines():
            temp_item = item.strip().split()
            key = temp_item[0]
            value = float(temp_item[1])
            src_dict[key] = value
     
    # sort the dict according to score field,that is second column(index=1)
    # return new list of tuple dict
    list_dict = sorted(src_dict.iteritems(),key=lambda x:x[1],reverse=True)
    
    # write the result into txt files
    with open(dest_dir+'score_sorted.txt','w') as f:
        for item in list_dict:
            f.write(item[0]+' '+str(item[1])+'\n')
            
    print('finish had costed is %d'%(time.time()-bagan))
    
    return list_dict           
            
if __name__=='__main__':
    sort_dict(r'1v4w_scores.txt',r'./')
    

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章