注释提示

时间:2016-03-21 18:42:36

标签: python annotations

好的,所以我已经在我的代码中注释了几乎所有内容,但是我稍微挣扎着注释我的for循环,我已经完成了所有这些,除了这两行我只是没有'我知道如何解释它对除了我以外的任何人都有意义。如果我能得到一些关于此的提示,那就太好了!

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]]'.

2 个答案:

答案 0 :(得分:0)

在一个你宣称字典:

 y = {} #

在另一个列表中:

 Positions = [] #

字典使用键存储对象。列表是元素堆栈(位置明智)。

答案 1 :(得分:0)

如果你要评论变量,那么注释应该解释变量包含(或者确切地说,因为代码的目的是填充这些变量,我们的目标变量将包含什么)和/或预期用于什么。既然我们没有看到这些数据用于任何事情,我会坚持前者:

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.