2016-12-07 131 views
-2

嘿傢伙我很困惑,很不確定爲什麼我的代碼不工作。我在這段代碼中正在做的是試圖從我的句子中的列表中找出某些單詞並輸出它在句子中重複的次數。Python如何計算每個詞彙單詞在句子中顯示的次數?

vocabulary =["in","on","to","www"] 
numwords = [0,0,0,0] 
mysentence = (" ʺAlso the sea tosses itself and breaks itself, and should any  sleeper fancying that he might find on the beach an answer to his doubts, a  sharer of his solitude, throw off his bedclothes and go down by himself to  walk on the sand, no image with semblance of serving and divine  promptitude comes readily to hand bringing the night to order and making  the world reflect the compass of the soul.ʺ) 
for word in mysentence.split(): 
       if (word == vocabulary): 
        else: 
        numwords[0] += 1 
       if(word == vocabulary): 
        else: 
        numwords[1] +=1 
       if (word == vocabulary): 
        else: 
        numwords [2] += 1 
       if (word == vocabulary): 
        else : 
        numwords [3] += 1 
       if (word == vocabulary): 
        else: 
        numwords [4] += 1 


print "total number of words : " + str(len(mysentence)) 
+1

請更新問題中的代碼。 –

+0

您給出的代碼示例的預期輸出是什麼? – 3novak

+0

此處的代碼是否與您的計算機上顯示的相同? –

回答

1

要做到這一點,最簡單的方法是使用collections.Counter計算所有詞語的句子,然後查找你感興趣的人。

from collections import Counter 
vocabulary =["in","on","to","www"] 
mysentence = "Also the sea tosses itself and breaks itself, and should any sleeper fancying that he might find on the beach an answer to his doubts, a sharer of his solitude, throw off his bedclothes and go down by himself to walk on the sand, no image with semblance of serving and divine promptitude comes readily to hand bringing the night to order and making the world reflect the compass of the soul." 
mysentence = mysentence.split() 
c = Counter(mysentence) 
numwords = [c[i] for i in vocabulary] 
print(numwords) 
+0

這是Pythonic的做法。對於一個初學者來說可能有點高級,但總體來說很好。列表理解是優雅的! – noumenal

-1

考慮問題的字符像「和」可能解析不好,除非已經指定了適當的編碼方案。

this_is_how_you_define_a_string = "The string goes here" 

# and thus: 

mysentence = "Also the sea tosses itself and breaks itself, and should any sleeper fancying that he might find on the beach an answer to his doubts, a sharer of his solitude, throw off his bedclothes and go down by himself to walk on the sand, no image with semblance of serving and divine promptitude comes readily to hand bringing the night to order and making the world reflect the compass of the soul." 

for v in vocabulary: 
    v in mysentence # Notice the indentation of 4 spaces 

該解決方案將返回TRUEFALSE如果V I罪mysentence。我想我將作爲練習如何積累價值觀。提示:TRUE == 1和FALSE = 0.您需要每個單詞v的真值的總和。

+0

這應該是某種發電機嗎?你缺少'yield'和某種'def'語句 –

1

想必你可以在列表中有重複的循環檢查,如果它在列表中,然後遞增計數器 - 示例實現可能看起來像

def find_word(word,string): 
    word_count = 0 
    for i in range(len(string)): 
     if list[i] == word: 
      word_count +=1 

這可能是有點低效率的,但我敢肯定,它可能是更容易理解你比collections.Counter :)

1

我會做這樣老老實實檢查:

for word in mysentence.split(): 
    if word in vocabulary: 
     numwords[vocabulary.index(word)] += 1 

因此整個代碼應該是這樣的:

vocabulary = ["in", "on", "to", "www"] 
numwords = [0, 0, 0, 0] 
mysentence = (" ʺAlso the sea tosses itself and breaks itself, and should any sleeper fancying that he might find on the beach an answer to his doubts, a sharer of his solitude, throw off his bedclothes and go down by himself to walk on the sand, no image with semblance of serving and divine promptitude comes readily to hand bringing the night to order and making the world reflect the compass of the soul.ʺ") 
for word in mysentence.replace('.', '').replace(',', '').split(): 
    if word in vocabulary: 
     numwords[vocabulary.index(word)] += 1 

print("total number of words : " + str(len(mysentence))) 

由於@Jacob建議,取代了「」和','字符也可以在分割之前應用,以避免任何可能的衝突。

相關問題