當前位置:成語大全網 - 新華字典 - python 的壹個關於字典的問題

python 的壹個關於字典的問題

"""Invert the dictionary

"""

freq= {}

#這段程序的作用是將用戶名,次數保存的數據轉換成

#次數,用戶名的保存方式

for (name, times) in count.items():

if times in freq:

#times已經存在,表示已經不是第壹用戶,freq[times]已經

#初始化為list,因此這裏可以調用append接口記錄新的用戶

freq[times].append(name)

else:

#不存在的話,則表示這個是第壹個用戶,因此需要初始化為

#[name],不然的話,上面就不能調用append接口了

freq[times] = [name] # why it's [name]

#print

#所有的數據都已經轉換完成,這裏是顯示部分

#首先對次數按照從低往高進行排序

for key in sorted(freq):

print key

#然後打印保存的用戶名即可

for name in freq[key]:

print " ", name

"""