妳清除了計數。
刪除“count.pop(tempKey)”這句話應該沒問題。
Dict.pop的用法如下
pop(鍵[,默認])
如果關鍵字在字典中,刪除它並返回它的值,否則返回默認值。如果沒有給定default並且關鍵字不在字典中,則會引發壹個KeyError。
PS:其實有更簡單的方法。
seq = ['a ',' hello ',' hello ',' hello ',' hello ',' hello ',' world ',' z ',' world ',' world ',' z']
keySet = set(seq)
temp = [(a,seq.count(a)) for a in keySet]
temp.sort(lambda x,y: cmp(x[1],y[1]))