I'm new to Python and I'm trying to know if code is possible :
chs = ch.split(' ')
d = { mot:ch.count(mot) for mot in chs if (mot not in d)}
These instructions aim to create a dictionary, based on my knowledge in other programming languages, I understand that count would be done every time a loop is iterated, so what I'm trying to do is to optimise and not do the count if the word is already counted.
Now the problem is that i get this error
NameError: free variable 'd' referenced before assignment in enclosing scope
which I totally understand, the variable d
is not set yet.
So, what I'm looking for is to know if there is some temporary variable that has the values before enclosure.
Aucun commentaire:
Enregistrer un commentaire