Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python for循环中的append函数不工作_Python_Loops_For Loop - Fatal编程技术网

Python for循环中的append函数不工作

Python for循环中的append函数不工作,python,loops,for-loop,Python,Loops,For Loop,我正在尝试写一个情感分析领域的代码。我有一本字典(.txt),里面对单词进行分级,例如“good,2”和“bad,-3”。现在我想让Python把一个给定句子中的积极和消极因素加起来。我的代码片段如下所示: text ='' result = [] for sentence in sent_tokenize(text): pos = 0 neg = 0 for word in word_tokenize(sentence): score = Dictio

我正在尝试写一个情感分析领域的代码。我有一本字典(.txt),里面对单词进行分级,例如“good,2”和“bad,-3”。现在我想让Python把一个给定句子中的积极和消极因素加起来。我的代码片段如下所示:

text =''

result = []
for sentence in sent_tokenize(text):
    pos = 0
    neg = 0
    for word in word_tokenize(sentence):
        score = Dictionary.get(word, 0)
        if score > 0:
            pos += score
            if score < 0:
                neg += score
                result.append([pos, neg])

for s in result: print(s)

print(result)
text=''
结果=[]
对于sent_tokenize(文本)中的句子:
pos=0
负=0
对于单词中的单词,标记化(句子):
分数=字典。获取(单词,0)
如果得分>0:
pos+=分数
如果得分<0:
neg+=分数
结果。追加([pos,neg])
对于结果中的:打印
打印(结果)
所以我希望结果是这样的:
[5,-6]
。 但我得到一个空结果:
[]

你知道我做错了什么吗?

分数不能同时小于或大于零:

if score > 0:
    pos += score
    if score < 0:
        neg += score
        result.append([pos, neg])
如果分数>0:
pos+=分数
如果得分<0:
neg+=分数
结果。追加([pos,neg])
将代码更改为:

result = []
for sentence in sent_tokenize(text):
    pos = 0
    neg = 0
    for word in word_tokenize(sentence):
        score = Dictionary.get(word, 0)
        if score > 0:
            pos += score
        if score < 0:
            neg += score
    result.append([pos, neg])
result=[]
对于sent_tokenize(文本)中的句子:
pos=0
负=0
对于单词中的单词,标记化(句子):
分数=字典。获取(单词,0)
如果得分>0:
pos+=分数
如果得分<0:
neg+=分数
结果。追加([pos,neg])
注意
结果的缩进。追加([pos,neg])
。这会给你一个新的机会
每个句子的一对
pos,neg

分数不能同时小于或大于零:

if score > 0:
    pos += score
    if score < 0:
        neg += score
        result.append([pos, neg])
如果分数>0:
pos+=分数
如果得分<0:
neg+=分数
结果。追加([pos,neg])
将代码更改为:

result = []
for sentence in sent_tokenize(text):
    pos = 0
    neg = 0
    for word in word_tokenize(sentence):
        score = Dictionary.get(word, 0)
        if score > 0:
            pos += score
        if score < 0:
            neg += score
    result.append([pos, neg])
result=[]
对于sent_tokenize(文本)中的句子:
pos=0
负=0
对于单词中的单词,标记化(句子):
分数=字典。获取(单词,0)
如果得分>0:
pos+=分数
如果得分<0:
neg+=分数
结果。追加([pos,neg])
注意
结果的缩进。追加([pos,neg])
。这会给你一个新的机会
每个句子都有一对
pos,neg

在哪里定义
sent\u tokenize()
word\u tokenize()
?在代码前面,我定义了它:调用函数的句子=word\u tokenize('.lower())和句子=sent\u tokenize('.lower()),没有定义它们。对不起,我以为我定义了它们?定义是什么样子的?如果调用函数,它们应该存在于某个地方。那可能是来自。。。从…导入*
。。。导入sent_tokenize、word_tokenize或
def sent_tokenize(…):
def word_tokenize(…):
您在哪里定义
sent_tokenize()
word_tokenize()
?在代码前面,我定义了它:调用函数的语句=word_tokenize('.lower())和语句=sent_tokenize('.lower()),没有定义。对不起,我以为我定义了他们?定义是什么样子的?如果调用函数,它们应该存在于某个地方。那可能是来自。。。从…导入*
。。。导入已发送标记化、word标记化或
def已发送标记化(…):
def word标记化(…):
感谢您的建议!不幸的是,我仍然得到一个空输出。。。我还需要更改其他内容吗?只需添加一些调试打印,以查看
语句
单词
的值。确保您实际执行循环。;)谢谢你的建议!不幸的是,我仍然得到一个空输出。。。我还需要更改其他内容吗?只需添加一些调试打印,以查看
语句
单词
的值。确保您实际执行循环。;)