Reputation: 72
Okay so I've annotated almost everything in my code, but I'm struggling slightly with annotating my for loop, I've got all of it except these two lines I just don't know how to explain with it making sense to anyone but me. Would be great if I could get some tips on this!
y = {} #
Positions = [] #
for i, word in enumerate (Sentence): #This starts a for look to go through every word within the variable 'Sentence' which is a list.
if not word in y: #This continues the code below if the word isn't found within the variable 'y'.
y[word] = (i+1) #This gives the word that wasn't found within the variable 'y' the next unused number plus 1 so that it doesn't confuse those unfamiliar with computer science starting at 0.
Positions = Positions + [y[word]] #This sets the variable 'Positions' to the variables 'Positions' and '[d[word]]'.
Upvotes: 0
Views: 95
Reputation: 279255
If you're going to comment a variable, then the comment should explain that the variable contains (or to be precise, since the purpose of the code is to populate these variables, our goal for what the variable will contain) and/or what it's expected to be used for. Since we don't see this data used for anything, I'll stick to the former:
y = {} # dictionary mapping words to the (1-based) index of their first occurrence in the sentence
Positions = [] # list containing, for each position in the sentence, the (1-based) index of the first occurrence of the word at that position.
Upvotes: 1
Reputation: 13465
In one you are declaring a dictionary:
y = {} #
in another a list:
Positions = [] #
Dictionaries store objects with keys. Lists are stacks of elements (position wise).
Upvotes: 1