Wie kann ich das beheben?syntax_error: Update für das Wörterbuch
# 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
Haftungsausschluss: Insgesamt Neuling in Python
Wie würde ich diese Zeile mit .update() schreiben? danke für die antwort :) –
wenn interviewte '' aus sammlungen importieren Counter def word_count (string): zurück Counter (string.split()) 'ist das akzeptabel? Wie hoch ist der Sprachgebrauch, der in einem Interview akzeptabel ist? Danke für die Tipps –
@MonaJalal: Ich würde sagen, dass es ein Maß an Vertrautheit mit der Standard-Bibliothek demonstriert.Ich würde auch weiter forschen, wenn sie über alle Funktionen von 'Counter() 'Bescheid wissen, fragen, was sonst vielleicht damit gemacht werden könnte, oder um mir zu zeigen, wie man dasselbe ohne' Counter() 'macht Sonde grundlegende Python-Kenntnisse. Alles hängt von der Position ab, für die Sie interviewen. –