2016-11-16 48 views
1

我是初學者尋求一些幫助。我正在嘗試編寫一個python程序,它會從.txt文件返回一個列表。顯示具有不同字符長度的單詞的數量。例如,「在列表中有五個單詞,三個或更少的字符。」計數列表中的字符數python

這是我到目前爲止有:

def count_lengths(text): 

    up_to_three = 0 
    four_or_five = 0 
    six_to_nine = 0 
    ten_or_more = 0 
    newtext = text.split(' ') 

def main(): 

    filename = "gb.txt" 
    text = readfile(filename) 
    word_lengths = count_lengths(text) 
    print(word_lengths) 

.txt文件轉換成一個列表後,我幾乎失去了。有人可以幫我解決這個問題嗎?

回答

0

也許最簡單的辦法是使用Counter

from collections import Counter 

text = 'Some text from your file that you have read into this variable' 

    print(sorted(map(len, text.split()))) 

    word_lengths = {} 

    # cumulate number of words 
    total = 0 
    for k,v in sorted(Counter(map(len, text.split())).items()): 
     total += v 
     word_lengths[k] = total 


    print(word_lengths) 
    # {8: 12, 3: 1, 4: 11} 
+0

你知道的更好:P你需要先排序:P –

+0

@JoranBeasley謝謝。 Soring added – Marcin

0

使用collections.Counter將產生與該鍵作爲字長度和值作爲在每個長度字的數目的dict樣的對象。

>>> s = 'hello this is a sentence with words of varying lengths' 

首先,跟蹤所有的字長:

>>> lengths = [len(word) for word in s.split()] 
>>> lengths 
[5, 4, 2, 1, 8, 4, 5, 2, 7, 7] 

然後,算多少單詞串以上發生在不同的長度:

>>> from collections import Counter 
>>> word_lengths = Counter(lengths) 
>>> word_lengths 
Counter({2: 2, 4: 2, 5: 2, 7: 2, 1: 1, 8: 1}) 

編輯:既然你想要累計總和,試試這個:

def count_lengths(text, n): 
    lengths = [len(word) for word in text.split()] 
    word_lengths = Counter(lengths) 
    # count the total number of words with lengths less than or equal to n 
    n_and_less_chars = sum(value for key, value in word_lengths.items() if key <= n) 
    return n_and_less_chars 

嘗試出來:

>>> print(count_lengths(s, 5)) 
7 

如果我們看一下上面的例子串中,我們可以看到有,實際上,7個字具有5個字符或更少。

+0

OPs需要累計總和,即有5個長度小於等於5的單詞。 – Marcin