2012-02-20 72 views
0

我有一段文字,我在python中創建了一本字典。它包含文字作爲關鍵字,文字中出現的文字作爲值的次數。該字典按值字段的值遞減排序。這裏是我的清單的片段:Python中的字典操作

[('the\n', 1644), ('and\n', 872), ('to\n', 729), ('a\n', 632), ('she\n', 541), 
('it\n', 530), ('of\n', 514), ('said\n', 462), ('i\n', 410), ('alice\n', 386), 
('in\n', 369), ('you\n', 365), ('was\n', 357), ('that\n', 280), ('as\n', 263), 
('her\n', 248), ('at\n', 212), ('on\n', 193), ('all\n', 182), ('with\n', 181), 
('had\n', 178), ('but\n', 170), ('for\n', 153), ('so\n', 151), ('be\n', 148), 
('not\n', 145), ('very\n', 144), ('what\n', 136), ('this\n', 134), 
('they\n', 130), ('little\n', 128), ('he\n', 120), ('out\n', 117), 
('is\n', 108), ... ] 

我想打印25個最常用的單詞。這很簡單,我已經完成了。下一部分是打印以字母「f」開頭的25個最常用的單詞。我如何找到它並將其附加到最常用的25個單詞列表中?

此外,我必須添加所有單詞的排名。例如,在我的字典中,「the」將被排名爲1,「和」2等等。我如何在單詞列表中添加一個排名?

回答

2

只是篩選使用列表理解:

f_words = [(word, freq) for (word, freq) in the_list if word.startswith('f')] 

由於原來的列表進行排序,所以這將是一個。然後,你可以切它讓高層25:f_words[:25]

+0

如果我想從1-25開始對這些文檔進行排名,我如何在關鍵字:值對列表中包含排名? – Nerd 2012-02-20 22:06:06

+0

您可以使用'enumerate(some_list,1)'獲取(索引,元素)對。 '1'表示起點,否則從0開始計數。 – tzaman 2012-02-20 22:48:02

3

一種選擇是使用itertools.ifilter()itertools.islice()

f_words = islice(ifilter(lambda x: x[0].startswith("f"), words), 25) 
for word, count in f_words: 
    print word.rstrip() 

相反的ifilter(),你也可以用生成器表達式:

f_words = islice((w for w, c in words if w.startswith("f")), 25) 
for word in f_words: 
    print word.rstrip() 

這兩種方法的優點是,您無需首先過濾整個列表 - 循環將在25個單詞後停止。