私はpythonコードを書いて、ログからキーを取得し、advert_sumで降下ソートを行い、ソートされた関数を呼び出すと、
sorted(dict, cmp=lambda x,y: cmp(adver_num), reverse=False)
それは報告しますnot adver_num
。どうすれば修正できますか?dict[].adver_num
? 私はいくつかの方法を試しましたが、それでも失敗しました。
import re
dict={}
class log:
def __init__(self,query_num, adver_num):
self.query_num = query_num
self.adver_num = adver_num
f = open('result.txt','w')
for line in open("test.log"):
count_result = 0
query_num = 0
match=re.search('.*qry=(.*?)qi.*rc=(.*?)dis',line).groups()
counts=match[1].split('|')
for count in counts:
count_result += int(count)
if match[0].strip():
if not dict.has_key(match[0]):
dict[match[0]] = log(1,count_result)
else:
query_num = dict[match[0]].query_num+1;
count_result = dict[match[0]].adver_num+count_result;
dict[match[0]] = log(query_num,count_result)
#f.write("%s\t%s\n"%(match[0],count_result))
sorted(dict,cmp=lambda x,y:cmp(adver_num),reverse=False)
for i in dict.keys():
f.write("%s\t%s\t%s\n"%(i,dict[i].query_num,dict[i].adver_num)