freq = dict() for line in open(filename, 'rb'): for c in line: freq.setdefault(c, 0) freq[c] += 1 # 頻度が少ないものから順に枝をまとめてツリー形成 wtree = [i for i in freq.items()] while len(wtree) > 1: # 出現頻度ワースト2の枝を見つけてまとめる wtree.sort(lambda a,b: cmp(b[1], a[1])) //ここをpython3用に変えたい b2, b1 = (wtree.pop(), wtree.pop()) wtree.append(((b1[0], b2[0]), b1[1] + b2[1]))
回答1件
あなたの回答
tips
プレビュー