我在nltk文档中找到了这段代码(http://www.nltk.org/_modules/nltk/sentiment/vader.html)
if (i < len(words_and_emoticons) - 1 and item.lower() == "kind" and \
words_and_emoticons[i+1].lower() == "of") or \
item.lower() in BOOSTER_DICT:
sentiments.append(valence)
continue
Run Code Online (Sandbox Code Playgroud)
有人可以解释这个if条件意味着什么吗?
行尾的反斜杠告诉Python将当前逻辑行扩展到下一个物理行.请参阅Python参考文档的" 行结构"部分:
2.1.5.明确的行加入
两个或多个物理线路可连接到使用反斜线字符(逻辑线
\),具体如下:当物理行以反斜杠不是字符串文字或注释的一部分端部,它被接合于以下形成一个单一的逻辑线,删除反斜杠和下面的行尾字符.例如:Run Code Online (Sandbox Code Playgroud)if 1900 < year < 2100 and 1 <= month <= 12 \ and 1 <= day <= 31 and 0 <= hour < 24 \ and 0 <= minute < 60 and 0 <= second < 60: # Looks like a valid date return 1
还可以选择使用隐式线连接,使用括号或括号或花括号; Python不会结束逻辑行,直到找到每个左括号或括号的匹配右括号或括号.这是推荐的代码样式,您找到的样本应该写为:
if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
words_and_emoticons[i+1].lower() == "of") or
item.lower() in BOOSTER_DICT):
sentiments.append(valence)
continue
Run Code Online (Sandbox Code Playgroud)
请参阅Python样式指南(PEP 8)(但请注意异常;某些Python语句不支持(...)括号,因此可以接受反斜杠).
请注意,Python不是唯一使用反斜杠进行行继续的编程语言; bash,C和C++预处理器语法,Falcon,Mathematica和Ruby也使用这种语法扩展行; 看维基百科.