Python 如何检查得到的值是否在增加/减少?
首先,让我具体说明几件事:Python 如何检查得到的值是否在增加/减少?,python,Python,首先,让我具体说明几件事: cleaned_example = [['I', 'horrible', 'physics', 'pretty', 'good', 'chemistry', 'excellent', 'math'], ['First', 'concerned', 'worried', 'scared'], ['What', 'started', 'bar', 'fight', 'turned', 'confrontation', 'finally', 'gang', 'war'],
cleaned_example = [['I', 'horrible', 'physics', 'pretty', 'good', 'chemistry', 'excellent', 'math'], ['First', 'concerned', 'worried', 'scared'], ['What', 'started', 'bar', 'fight', 'turned', 'confrontation', 'finally', 'gang', 'war'], ['Every', 'hill', 'mountain', 'shall', 'made', 'low', 'rough', 'places', 'made', 'plain,', 'crooked', 'places', 'made', 'straight'], ['This', 'blessed', 'plot', 'earth', 'realm', 'England']]
但实际上是由20组单词组成的
我正在使用pattern.en中的情感函数,我想看看cleaned_示例[0],cleaned_示例[1]中单词的情感值。。。。正在增加或减少。情感函数输出形式(a,b)的两个值,但我只对其中的第一个值感兴趣
以下是我迄今为止所做的工作。我遇到两个问题。首先,我得到了40个输出,而我应该只有20个。其次,所有这些输出都是“否”,因此它是非常无用的
for index in range(len(cleaned_example)):
for position in range(len(cleaned_example[index])-1):
if sentiment(cleaned_example[index][position][0]) < sentiment(cleaned_example[index][position+1][1]):
print('yes')
else:
print('no')
用于范围内的索引(len(示例)):
对于范围内的位置(len(例如[索引])-1):
如果情绪(已清理示例[指数][位置][0])<情绪(已清理示例[指数][位置+1][1]):
打印('是')
其他:
打印('否')
提前谢谢 因为您有参数
(a,b)
,并且您只关心a
,所以在选择这些参数时,您总是希望[0]
;我怀疑你的括号也放错地方了。也就是说,您的差异行应为:
if sentiment(cleaned_example[index][position])[0] < sentiment(cleaned_example[index][position+1])[0]:
最后,在这里,你几乎每个单词都要打两次情绪电话。如果这是一个问题,那么你应该重新构造你的代码一点。考虑到这一点,最好从以下内容开始:
for word_list in cleaned_example:
sentiments = [sentiment(word)[0] for word in word_list]
for i in range(len(sentiments)):
if sentiments[i] < sentiments[i+1]:
print('yes')
else:
print('no')
清理后的单词列表示例中的:
情绪=[情绪(单词)[0]表示单词列表中的单词]
对于范围内的i(len(情绪)):
如果情感[i]<情感[i+1]:
打印('是')
其他:
打印('否')
在避免索引和使用更有意义的变量名方面比tom更进一步(我在谷歌上搜索了这件事):
对于示例中的句子:
对于单词,zip中的下一个单词(句子,句子[1:]):
如果情绪(单词)[0]<情绪(下一个单词)[0]:
打印('是')
其他:
打印('否')
例如,如果句子是['a','b','c','d']
,那么句子[1:
是['b','c','d']
,压缩它们会给你单词对('a','b')
,('b','c')
和('c','d')
我不知道为什么你认为你应该只得到20个输出(顺便说一句,我得到36个,而不是40个)。我怀疑你的工作水平不对,应该在句子上使用情绪
,而不是在单词上?请注意我是如何命名变量名的,好的名称确实可以帮助您理解代码。三重索引不太多。如果你只希望得到20个输出,那么假设每组单词只需要“是”或“否”是正确的吗?我不确定你到底想如何衡量这一点,因为你只需比较cleaned\u example
中每组单词列表中第一个单词和最后一个单词的情绪即可
for words in cleaned_example:
if sentiment(words[0])[0] < sentiment(words[-1])[0]:
print "Increasing"
else:
print "Decreasing"
对于示例中的单词:
如果情绪(词语[0])[0]<情绪(词语[-1])[0]:
打印“增加”
其他:
打印“递减”
另一种方法是计算每组单词的“是”和“否”的数量
for words in cleaned_example:
yes = 0
no = 0
for word1, word2 in zip(words, words[1:]):
if sentiment(word1)[0] < sentiment(word2)[0]:
yes = yes + 1
else:
no = no + 1
if yes > no:
print "yes"
elif yes < no:
print "no"
else:
print "`yes` = `no`"
对于示例中的单词:
是=0
否=0
对于zip中的word1和word2(words,words[1:]):
如果情绪(单词1)[0]<情绪(单词2)[0]:
是=是+1
其他:
否=否+1
如果是>否:
打印“是”
如果是<否:
打印“否”
其他:
打印“`yes`=`no`”
另一种方法是获得集合的平均情绪,并将其与第一个单词的情绪进行比较
import numpy as np
for words in cleaned_example:
a_values = []
for word in words:
a_values.append(sentiment(word)[0])
if sentiment(words[0])[0] < np.mean(a_values):
print "Increasing"
将numpy导入为np
对于示例中的单词:
a_值=[]
用文字表示:
a_values.append(情绪(词)[0])
如果情绪(单词[0])[0]
谢谢,这很好用。我没想过这样使用平均值。
for words in cleaned_example:
yes = 0
no = 0
for word1, word2 in zip(words, words[1:]):
if sentiment(word1)[0] < sentiment(word2)[0]:
yes = yes + 1
else:
no = no + 1
if yes > no:
print "yes"
elif yes < no:
print "no"
else:
print "`yes` = `no`"
import numpy as np
for words in cleaned_example:
a_values = []
for word in words:
a_values.append(sentiment(word)[0])
if sentiment(words[0])[0] < np.mean(a_values):
print "Increasing"