懵懂的菜鸟

Stay hungry,Stay foolish.

导航

pyhthon lambda

 lambda x:x+1(1)

  >>>2   

  可以这样认为,lambda作为一个表达式,定义了一个匿名函数,上例的代码x为入口参数和出口参数,x+1为函数体,(1)为x的入口初始值,

用函数来表示为:

1 def g(x):
2 return x+1

  非常容易理解,在这里lambda简化了函数定义的书写形式。是代码更为简洁,但是使用函数的定义方式更为直观,易理解。

 

simListSorted=sorted(simList,key=lambda jj:jj[1],reverse=True)#此段代码表示对元组列表进行倒序排列

 

 

 1 if __name__=='__main__':
 2     # test()
 3     # m=4;n=4
 4     # RatingMat=np.zeros((m,n),int)
 5     # print(type(RatingMat))
 6     # print(RatingMat)
 7     # obj=pd.read_csv('./data/rating11.csv',header=None)
 8     
 9     # print(type(obj))
10     # m,n=943,1682
11     # RatingMat=np.zeros((m,n),int)
12     userRatingTable=pd.read_csv('./data/RatingTable.csv',header=None)
13     allUserSim={}
14     userList=[]
15     for i in range(5):
16         for j in range(5):
17             if i==j:
18                 userList.append((j,0))
19             else:
20                 userList.append((j,pearson(userRatingTable.ix[i],userRatingTable.ix[j])))
21         print("第%d行原始"%i,userList)
22         sortedUserList=sorted(userList,key=lambda x:x[1],reverse=True)
23         print("第%d行排列"%i,sortedUserList)
24         allUserSim[i]=sortedUserList
25         print("第%d行之后"%i,allUserSim)

运行结果:

第0行原始 [(0, 0), (1, 0.10632192973557733), (2, -0.016423927384195555), (3, 0.021715884544321169), (4, 0.30259213811724522)]
第0行排列 [(4, 0.30259213811724522), (1, 0.10632192973557733), (3, 0.021715884544321169), (0, 0), (2, -0.016423927384195555)]
第0行之后 {0: [(4, 0.30259213811724522), (1, 0.10632192973557733), (3, 0.021715884544321169), (0, 0), (2, -0.016423927384195555)]}
第1行原始 [(0, 0), (1, 0.10632192973557733), (2, -0.016423927384195555), (3, 0.021715884544321169), (4, 0.30259213811724522), (0, 0.10632192973557732), (1, 0), (2, 0.082680168542824992), (3, 0.16032261105387982), (4, 0.020217807303362857)]
第1行排列 [(4, 0.30259213811724522), (3, 0.16032261105387982), (1, 0.10632192973557733), (0, 0.10632192973557732), (2, 0.082680168542824992), (3, 0.021715884544321169), (4, 0.020217807303362857), (0, 0), (1, 0), (2, -0.016423927384195555)]
第1行之后 {0: [(4, 0.30259213811724522), (1, 0.10632192973557733), (3, 0.021715884544321169), (0, 0), (2, -0.016423927384195555)], 1: [(4, 0.30259213811724522), (3, 0.16032261105387982), (1, 0.10632192973557733), (0, 0.10632192973557732), (2, 0.082680168542824992), (3, 0.021715884544321169), (4, 0.020217807303362857), (0, 0), (1, 0), (2, -0.016423927384195555)]}
第2行原始 [(0, 0), (1, 0.10632192973557733), (2, -0.016423927384195555), (3, 0.021715884544321169), (4, 0.30259213811724522), (0, 0.10632192973557732), (1, 0), (2, 0.082680168542824992), (3, 0.16032261105387982), (4, 0.020217807303362857), (0, -0.016423927384195552), (1, 0.082680168542824992), (2, 0), (3, 0.3316738875790407), (4, -0.028321648634076626)]
第2行排列 [(3, 0.3316738875790407), (4, 0.30259213811724522), (3, 0.16032261105387982), (1, 0.10632192973557733), (0, 0.10632192973557732), (2, 0.082680168542824992), (1, 0.082680168542824992), (3, 0.021715884544321169), (4, 0.020217807303362857), (0, 0), (1, 0), (2, 0), (0, -0.016423927384195552), (2, -0.016423927384195555), (4, -0.028321648634076626)]
第2行之后 {0: [(4, 0.30259213811724522), (1, 0.10632192973557733), (3, 0.021715884544321169), (0, 0), (2, -0.016423927384195555)], 1: [(4, 0.30259213811724522), (3, 0.16032261105387982), (1, 0.10632192973557733), (0, 0.10632192973557732), (2, 0.082680168542824992), (3, 0.021715884544321169), (4, 0.020217807303362857), (0, 0), (1, 0), (2, -0.016423927384195555)], 2: [(3, 0.3316738875790407), (4, 0.30259213811724522), (3, 0.16032261105387982), (1, 0.10632192973557733), (0, 0.10632192973557732), (2, 0.082680168542824992), (1, 0.082680168542824992), (3, 0.021715884544321169), (4, 0.020217807303362857), (0, 0), (1, 0), (2, 0), (0, -0.016423927384195552), (2, -0.016423927384195555), (4, -0.028321648634076626)]}
第3行原始 [(0, 0), (1, 0.10632192973557733), (2, -0.016423927384195555), (3, 0.021715884544321169), (4, 0.30259213811724522), (0, 0.10632192973557732), (1, 0), (2, 0.082680168542824992), (3, 0.16032261105387982), (4, 0.020217807303362857), (0, -0.016423927384195552), (1, 0.082680168542824992), (2, 0), (3, 0.3316738875790407), (4, -0.028321648634076626), (0, 0.021715884544321169), (1, 0.16032261105387982), (2, 0.3316738875790407), (3, 0), (4, -0.0024255834332894584)]
第3行排列 [(3, 0.3316738875790407), (2, 0.3316738875790407), (4, 0.30259213811724522), (3, 0.16032261105387982), (1, 0.16032261105387982), (1, 0.10632192973557733), (0, 0.10632192973557732), (2, 0.082680168542824992), (1, 0.082680168542824992), (3, 0.021715884544321169), (0, 0.021715884544321169), (4, 0.020217807303362857), (0, 0), (1, 0), (2, 0), (3, 0), (4, -0.0024255834332894584), (0, -0.016423927384195552), (2, -0.016423927384195555), (4, -0.028321648634076626)]
第3行之后 {0: [(4, 0.30259213811724522), (1, 0.10632192973557733), (3, 0.021715884544321169), (0, 0), (2, -0.016423927384195555)], 1: [(4, 0.30259213811724522), (3, 0.16032261105387982), (1, 0.10632192973557733), (0, 0.10632192973557732), (2, 0.082680168542824992), (3, 0.021715884544321169), (4, 0.020217807303362857), (0, 0), (1, 0), (2, -0.016423927384195555)], 2: [(3, 0.3316738875790407), (4, 0.30259213811724522), (3, 0.16032261105387982), (1, 0.10632192973557733), (0, 0.10632192973557732), (2, 0.082680168542824992), (1, 0.082680168542824992), (3, 0.021715884544321169), (4, 0.020217807303362857), (0, 0), (1, 0), (2, 0), (0, -0.016423927384195552), (2, -0.016423927384195555), (4, -0.028321648634076626)], 3: [(3, 0.3316738875790407), (2, 0.3316738875790407), (4, 0.30259213811724522), (3, 0.16032261105387982), (1, 0.16032261105387982), (1, 0.10632192973557733), (0, 0.10632192973557732), (2, 0.082680168542824992), (1, 0.082680168542824992), (3, 0.021715884544321169), (0, 0.021715884544321169), (4, 0.020217807303362857), (0, 0), (1, 0), (2, 0), (3, 0), (4, -0.0024255834332894584), (0, -0.016423927384195552), (2, -0.016423927384195555), (4, -0.028321648634076626)]}
第4行原始 [(0, 0), (1, 0.10632192973557733), (2, -0.016423927384195555), (3, 0.021715884544321169), (4, 0.30259213811724522), (0, 0.10632192973557732), (1, 0), (2, 0.082680168542824992), (3, 0.16032261105387982), (4, 0.020217807303362857), (0, -0.016423927384195552), (1, 0.082680168542824992), (2, 0), (3, 0.3316738875790407), (4, -0.028321648634076626), (0, 0.021715884544321169), (1, 0.16032261105387982), (2, 0.3316738875790407), (3, 0), (4, -0.0024255834332894584), (0, 0.30259213811724528), (1, 0.020217807303362853), (2, -0.028321648634076626), (3, -0.0024255834332894584), (4, 0)]
第4行排列 [(3, 0.3316738875790407), (2, 0.3316738875790407), (0, 0.30259213811724528), (4, 0.30259213811724522), (3, 0.16032261105387982), (1, 0.16032261105387982), (1, 0.10632192973557733), (0, 0.10632192973557732), (2, 0.082680168542824992), (1, 0.082680168542824992), (3, 0.021715884544321169), (0, 0.021715884544321169), (4, 0.020217807303362857), (1, 0.020217807303362853), (0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (4, -0.0024255834332894584), (3, -0.0024255834332894584), (0, -0.016423927384195552), (2, -0.016423927384195555), (4, -0.028321648634076626), (2, -0.028321648634076626)]
第4行之后 {0: [(4, 0.30259213811724522), (1, 0.10632192973557733), (3, 0.021715884544321169), (0, 0), (2, -0.016423927384195555)], 1: [(4, 0.30259213811724522), (3, 0.16032261105387982), (1, 0.10632192973557733), (0, 0.10632192973557732), (2, 0.082680168542824992), (3, 0.021715884544321169), (4, 0.020217807303362857), (0, 0), (1, 0), (2, -0.016423927384195555)], 2: [(3, 0.3316738875790407), (4, 0.30259213811724522), (3, 0.16032261105387982), (1, 0.10632192973557733), (0, 0.10632192973557732), (2, 0.082680168542824992), (1, 0.082680168542824992), (3, 0.021715884544321169), (4, 0.020217807303362857), (0, 0), (1, 0), (2, 0), (0, -0.016423927384195552), (2, -0.016423927384195555), (4, -0.028321648634076626)], 3: [(3, 0.3316738875790407), (2, 0.3316738875790407), (4, 0.30259213811724522), (3, 0.16032261105387982), (1, 0.16032261105387982), (1, 0.10632192973557733), (0, 0.10632192973557732), (2, 0.082680168542824992), (1, 0.082680168542824992), (3, 0.021715884544321169), (0, 0.021715884544321169), (4, 0.020217807303362857), (0, 0), (1, 0), (2, 0), (3, 0), (4, -0.0024255834332894584), (0, -0.016423927384195552), (2, -0.016423927384195555), (4, -0.028321648634076626)], 4: [(3, 0.3316738875790407), (2, 0.3316738875790407), (0, 0.30259213811724528), (4, 0.30259213811724522), (3, 0.16032261105387982), (1, 0.16032261105387982), (1, 0.10632192973557733), (0, 0.10632192973557732), (2, 0.082680168542824992), (1, 0.082680168542824992), (3, 0.021715884544321169), (0, 0.021715884544321169), (4, 0.020217807303362857), (1, 0.020217807303362853), (0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (4, -0.0024255834332894584), (3, -0.0024255834332894584), (0, -0.016423927384195552), (2, -0.016423927384195555), (4, -0.028321648634076626), (2, -0.028321648634076626)]}
[Finished in 1.2s]

 

posted on 2017-08-15 22:19  懵懂的菜鸟  阅读(401)  评论(0编辑  收藏  举报