どうすれば修正できますか?syntax_error:辞書の更新
# E.g. word_count("I am that I am") gets back a dictionary like:
# {'i': 2, 'am': 2, 'that': 1}
# Lowercase the string to make it easier.
# Using .split() on the sentence will give you a list of words.
# In a for loop of that list, you'll have a word that you can
# check for inclusion in the dict (with "if word in dict"-style syntax).
# Or add it to the dict with something like word_dict[word] = 1.
def word_count(string):
word_list = string.split()
word_dict = {}
for word in word_list:
if word in word_dict:
word_dict.update(word:word_dict(word)+1)
else:
word_dict[word]=1
return word_dict
免責事項:辞書内のキーを更新するにはPython
.update()を使用してその行を書く方法を教えてください。お返事ありがとうございます:) –
インタビュアーが 'コレクションからインポートを使用する場合Counter def word_count(string): return counter(string.split())'は受け入れ可能ですか?面接で受け入れられる言語のレベルはどの程度ですか?ヒントをありがとう –
@MonaJalal:標準ライブラリに精通していることを示しています。私は 'Counter()'のすべての機能を知っているかどうかをさらに調べ、おそらくそれを使って何ができるか質問したり、Counter()を使わずに同じことをする方法プローブの基本的なPythonの知識。それはあなたがインタビューしているポジションによって異なります。 –