我有這段代碼。這個想法是從一個字符串中獲得最常出現的模態動詞。例如,如果'can'出現兩次,並且比其餘的更多,則該函數應該返回'can',或者如果沒有模態動詞存在,則返回'none'。試圖在句子中獲得最出現的模態動詞
def check_modals(s):
modals = ['can', 'could', 'may', 'might', 'must', 'will', "should", "would"]
from collections import Counter
Counter([modal for modals, modal in s])
counts = Counter(modals)
c = counts.most_common(1)
return{c}
還是一個python新手。任何幫助將不勝感激。
喜!感謝您的答覆。我怎樣才能讓它返回只是'可能'作爲一個字或字符串。 – user3078335 2014-12-02 20:14:00
@ user3078335查看我的編輯,你可以直接編制索引'[0]'獲得'most_common'中的第一項,然後''0]'獲得該元組的第一個元素。 – CoryKramer 2014-12-02 20:16:22
非常感謝!還有一件事,'.split'是做什麼的? – user3078335 2014-12-02 20:20:43