1. 程式人生 > >【python】常用函式

【python】常用函式

使用list生成dict(可指定單條長度和資料型別,splen為4即為list中每4行組成dict中一條)

def list2dict(srclist,splen,datatype):# datatype: 0-str 1-int 2-float
    dstdict={}
    for i in range(0,int(len(srclist)/splen)):
        dstdict[srclist[splen*i]]=[]
        for j in range(1,splen):
            if datatype==0:
                dstdict[srclist[splen
*i]].append(srclist[splen*i+j]) elif datatype==1: dstdict[srclist[splen*i]].append(int(srclist[splen*i+j])) elif datatype==2: dstdict[srclist[splen*i]].append(float(srclist[splen*i+j])) else: print 'please choose the datatype
' return False return dstdict

 

計算兩個list的距離(注意不能用numpy的linalg.norm,因為用numpy的linalg.norm的話如果兩個list相等就會報錯(因為求了0向量的範數))

def cal_list_dist(srclist1,srclist2):
    if len(srclist1)!=len(srclist2):
        print 'error in cal_list_dict, len not equal!'
        return False
    listerror
=0 for i in range(0,len(srclist1)): listerror+=pow(srclist1[i]-srclist2[i],2) return pow(listerror,0.5)

 

比較按行存的label的誤差

例如可以比較這樣的list中名稱相同的訓練樣本的誤差:

 

 

 

將黃色處生成dict的間隔改為3,則可以比較這樣的list

 

 

import sys


def list2dict(srclist,splen,datatype):# datatype: 0-str 1-int 2-float
    dstdict={}
    for i in range(0,int(len(srclist)/splen)):
        dstdict[srclist[splen*i]]=[]
        for j in range(1,splen):
            if datatype==0:
                dstdict[srclist[splen*i]].append(srclist[splen*i+j])
            elif datatype==1:
                dstdict[srclist[splen*i]].append(int(srclist[splen*i+j]))
            elif datatype==2:
                dstdict[srclist[splen*i]].append(float(srclist[splen*i+j]))
            else:
                print 'please choose the datatype'
                return False
    return dstdict


def cal_list_dist(srclist1,srclist2):
    if len(srclist1)!=len(srclist2):
        print 'error in cal_list_dict, len not equal!'
        return False
    listerror=0
    for i in range(0,len(srclist1)):
         listerror+=pow(srclist1[i]-srclist2[i],2)
    return pow(listerror,0.5)

p_name=sys.argv[1]

with open('./10_10_662_withcam0/'+p_name) as f:
    withcam0_list=f.read().splitlines()

with open('./10_10_662_nocam0/'+p_name) as f:
    nocam0_list=f.read().splitlines()


withcam0_dict=list2dict(withcam0_list,4,2)

nocam0_dict=list2dict(nocam0_list,4,2)

sumerror=0
sumcount=0
for elem in withcam0_dict:
    if elem in nocam0_dict:
        sumerror+=cal_list_dist(withcam0_dict[elem],nocam0_dict[elem])
        sumcount+=1
print sumerror/sumcount