2016-12-01 74 views
0

我的功能應該是:從字典中計算單詞嗎?

  • 作爲推文的一個參數。
    • 此推文可能涉及數字,單詞,標籤,鏈接和標點符號。
  • 第二個參數是包含在其計數的話在字符串鳴叫,不顧包括hashtag的,提到,鏈接的字典,和標點符號。

該函數將字典中的所有單個單詞作爲小寫字母返回,沒有任何標點符號。

如果推文有Don't那麼字典會將其計爲dont

這裏是我的功能:

def count_words(tweet, num_words): 
''' (str, dict of {str: int}) -> None 
Return a NoneType that updates the count of words in the dictionary. 

>>> count_words('We have made too much progress', num_words) 
>>> num_words 
{'we': 1, 'have': 1, 'made': 1, 'too': 1, 'much': 1, 'progress': 1} 
>>> count_words("@utmandrew Don't you wish you could vote? #MakeAmericaGreatAgain", num_words) 
>>> num_words 
{'dont': 1, 'wish': 1, 'you': 2, 'could': 1, 'vote': 1} 
>>> count_words('I am fighting for you! #FollowTheMoney', num_words) 
>>> num_words 
{'i': 1, 'am': 1, 'fighting': 1, 'for': 1, 'you': 1} 
>>> count_words('', num_words) 
>>> num_words 
{'': 0} 
''' 
+0

'我不知道,如果我正在做這個功能',我們不確定,因爲我們不能看到你做了什麼,並嘗試......不要只是拋出對我們來說似乎是作業問題,期望我們寫你的代碼您。我們回答具體問題,並且非常樂意幫助那些努力解決問題的人。 – MooingRawr

+0

對不起,我的意思是說,我該如何爲這個特定功能編寫正確的return語句!或者只是提示? – vrrnki

+0

請閱讀#MooingRawr的評論。您的回覆與您的原始信息一樣,沒有任何可用的信息和有意義的問題。除非你能提出一個好問題,否則你不能得到一個好的答案。 – jwpfox

回答

0

我可能會誤解你的問題,但如果要更新詞典中,你能做到這樣:

d = {} 
def update_dict(tweet): 
    for i in tweet.split(): 
     if i not in d: 
      d[i] = 1 
     else: 
      d[i] += 1 
    return d