我需要帮助找到一个快捷方式,从一个频率字典中构建一个按频率排序的列表。我能够通过将每个元素附加到一个列表中,然后将每个列表附加到“列表的列表”中来构建一个列表的列表(仅在频率为1-3时容易),但是如果我有高达100或更多的频率呢?!必须有更好的方法。
dictionary = {'ab':2, 'bc':3, 'cd':1, 'de':1, 'ef':3, 'fg':1, 'gh':2}
list_1 = []
list_2 = []
list_3 = []
list_of_lists = []
for key, value in dictionary.items():
if value == 1:
list_1.append(key)
for key, value in dictionary.items():
if value == 2:
list_2.append(key)
for key, value in dictionary.items():
if value == 3:
list_3.append(key)
list_of_lists.append(list_1)
list_of_lists.append(list_2)
list_of_lists.append(list_3)
print list_of_lists
在Python中,复制的格式如下:
[['de', 'cd', 'fg'], ['ab', 'gh'], ['ef', 'bc']]
这正是我想要的,但对于包含100,000多个单词和100+频率的语料库来说,这种方法不起作用。请帮助我找到一种更好、更简便的方法来构建我的列表。