显示 "variable" 而不是数字 Python
Showing up the "variable" not the number Python
我是新手。抱歉这个愚蠢的问题。
我正在尝试统计许多 .txt 文件(3979 个文件)中的字数,例如 "Positive"、"Negative" 和 "Neutral"。我数过之后,我想挑一个数最多的。
假设在给定的文本中,有 5 个正面、1 个负面和 0 个中性。
我把它放在一个列表里比如
myRev = [Positive, Negative, Neutral]
那如果我用
largest = max(myRev)
当我调用 largest
时,它会给出
5
在这种情况下,我希望显示的值是Positive
、Negative
或Neutral
(>>>这个是我的主要问题)
之后,我想将 Positive
、Negative
或 Neutral
放入另一个列表中,我像这样追加...
ReviewList = []
ReviewList.append(maxNr)
我希望如果我调用 ReviewList
变成类似
Reviewlist = [Positive, Negative, Neutral, Neutral,... Neutral]
等等..
我能为此做什么?我很无能,并尝试尽可能多地阅读,但我对此越来越困惑...
这是我的 - 不太自信 - 代码:
listOfReview = []
for i in xrange(0,3979):
f = open("ReviewsOutput%i.txt" %i, "r")
myOutput = f.read()
Positive = myOutput.count("Positive")
Negative = myOutput.count("Negative")
Neutral = myOutput.count("Neutral")
myRev = [Positive, Negative, Neutral]
largest = max(myRev)
listOfReview.append(largest)
f.close
您可以使用字典 {'positive':5, 'negative':1, 'negative':0}
>>> review = {'positive':5, 'negative':1, 'negative':0}
>>> maxNr = max(review, key=review.get)
>>> maxNr
'positive'
>>> ReviewList = []
>>> ReviewList.append(maxNr)
>>> ReviewList
['positive']
编辑:
你可以试试这个:
listOfReview = []
review = {'Positive':0, 'Negative':0, 'Neutral':0}
for i in xrange(0,3979):
f = open("ReviewsOutput%i.txt" %i, "r")
myOutput = f.read()
review['Positive'] = myOutput.count("Positive")
review['Negative'] = myOutput.count("Negative")
review['Neutral'] = myOutput.count("Neutral")
largest = max(review, key=review.get)
listOfReview.append(largest)
f.close
您也可以通过调用review[largest]来获取评论数。
我是新手。抱歉这个愚蠢的问题。
我正在尝试统计许多 .txt 文件(3979 个文件)中的字数,例如 "Positive"、"Negative" 和 "Neutral"。我数过之后,我想挑一个数最多的。
假设在给定的文本中,有 5 个正面、1 个负面和 0 个中性。
我把它放在一个列表里比如
myRev = [Positive, Negative, Neutral]
那如果我用
largest = max(myRev)
当我调用 largest
时,它会给出
5
在这种情况下,我希望显示的值是Positive
、Negative
或Neutral
(>>>这个是我的主要问题)
之后,我想将 Positive
、Negative
或 Neutral
放入另一个列表中,我像这样追加...
ReviewList = []
ReviewList.append(maxNr)
我希望如果我调用 ReviewList
变成类似
Reviewlist = [Positive, Negative, Neutral, Neutral,... Neutral]
等等..
我能为此做什么?我很无能,并尝试尽可能多地阅读,但我对此越来越困惑...
这是我的 - 不太自信 - 代码:
listOfReview = []
for i in xrange(0,3979):
f = open("ReviewsOutput%i.txt" %i, "r")
myOutput = f.read()
Positive = myOutput.count("Positive")
Negative = myOutput.count("Negative")
Neutral = myOutput.count("Neutral")
myRev = [Positive, Negative, Neutral]
largest = max(myRev)
listOfReview.append(largest)
f.close
您可以使用字典 {'positive':5, 'negative':1, 'negative':0}
>>> review = {'positive':5, 'negative':1, 'negative':0}
>>> maxNr = max(review, key=review.get)
>>> maxNr
'positive'
>>> ReviewList = []
>>> ReviewList.append(maxNr)
>>> ReviewList
['positive']
编辑: 你可以试试这个:
listOfReview = []
review = {'Positive':0, 'Negative':0, 'Neutral':0}
for i in xrange(0,3979):
f = open("ReviewsOutput%i.txt" %i, "r")
myOutput = f.read()
review['Positive'] = myOutput.count("Positive")
review['Negative'] = myOutput.count("Negative")
review['Neutral'] = myOutput.count("Neutral")
largest = max(review, key=review.get)
listOfReview.append(largest)
f.close
您也可以通过调用review[largest]来获取评论数。