Python 在一个列表中找到唯一的单词,统计它们,同时跟踪另一个变量

Python 在一个列表中找到唯一的单词,统计它们,同时跟踪另一个变量,python,Python,如果给出如下所示的txt文件,我需要输出: 这部电影太棒了! 4年度最佳独立音乐奖。 这部电影还行。 有史以来最糟糕的表演 5奥斯卡电影 从开始到结束,有2股腐烂的气味 最差电影 输入一个短语来测试:电影太棒了 *“电影”出现4次,平均得分为3.5分 *“was”出现2次,平均得分为4.0 *“惊人”出现1次,平均得分为5.0分 这个短语的平均分是:4.16667 这是一个积极的短语 到目前为止,我已经能够创建一个独特单词的列表,但我一直在尝试不同的方法来配置计数器变量,但没有成功。我也不知道如

如果给出如下所示的txt文件,我需要输出: 这部电影太棒了! 4年度最佳独立音乐奖。 这部电影还行。 有史以来最糟糕的表演 5奥斯卡电影 从开始到结束,有2股腐烂的气味 最差电影

输入一个短语来测试:电影太棒了 *“电影”出现4次,平均得分为3.5分 *“was”出现2次,平均得分为4.0 *“惊人”出现1次,平均得分为5.0分 这个短语的平均分是:4.16667 这是一个积极的短语

到目前为止,我已经能够创建一个独特单词的列表,但我一直在尝试不同的方法来配置计数器变量,但没有成功。我也不知道如何使用索引来跟踪评分,因为我把它们分割成单个单词。有没有一种方法可以在不把所有的评论变成一个大的单词列表的情况下计算出独特的单词

#remove punctuations and store the cleaned data
clean_data=''
punc = '''!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~'''

split_data = data.split('\n')
#iterate over every character to check if it's punctuation
for character in data:
    if character not in punc:
       clean_data += character
    else:
        clean_data += ''


#store the cleaned reviews into a list of reviews
cleaned_str = clean_data.split('\n')


#seperate the ratings and reviews into two seperate lists
#they can be matched via index
score = []
reviews = []
for i in cleaned_str:
    score.append(int(i[0]))
    new_string = i[2:]
    reviews.append(new_string)




words = {}
unique = []
repeats = []
counter = 0 
#add all the words in all the reviews to one long string
#then turn into a list with all the words
reviews_words = ''
for i in reviews:
    reviews_words += str(i) + ' '
    word_list = reviews_words.split()

for word in word_list:
    if word not in unique:
        unique.append(word)
        counter +=1 
        repeats.append(counter)
    
    else:
        counter += 1
        repeats.append(counter)
    
#删除标点符号并存储清理后的数据
清洁_数据=“”
punc=''''''!"#$%&'()*+,-./:;?@[\]^_`{|}~'''
split_data=data.split('\n')
#迭代每个字符以检查它是否是标点符号
对于数据中的字符:
如果字符不在punc中:
清除数据+=字符
其他:
清洁_数据+=“”
#将清理后的评论存储到评论列表中
cleaned_str=clean_data.split('\n')
#将评级和评论分为两个单独的列表
#它们可以通过索引进行匹配
分数=[]
评论=[]
对于我所在的城市:
score.append(int(i[0]))
新字符串=i[2:]
reviews.append(新字符串)
单词={}
唯一=[]
重复=[]
计数器=0
#将所有评论中的所有单词添加到一个长字符串中
#然后把所有的单词列成一个列表
评论词=“”
就我而言,在评论中:
复习单词+=str(i)+”
word\u list=reviews\u words.split()
对于word\u列表中的word:
如果单词不是唯一的:
唯一。追加(word)
计数器+=1
重复。追加(计数器)
其他:
计数器+=1
重复。追加(计数器)
如果您能给我一些提示,我将不胜感激!我不想用导入的软件包来解决这个问题:)

试试这个:

PUNCTUATION = r'''!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~'''


def remove_punctuation(string):
    new_string = []
    for character in string:
        if character not in PUNCTUATION:
            new_string.append(character)

    return "".join(new_string)


scores = {}
counts = {}


with open('text.txt', 'r') as data:
    lines = data.readlines()


for line in lines:
    line = remove_punctuation(line)

    words = line.split()

    # Removes first 'word' from line which is the number rating
    score = int(words.pop(0))

    for word in words:
        if word in scores:
            scores[word] += score
            counts[word] += 1
        else:
            scores[word] = score
            counts[word] = 1

for word in scores:
    scores[word] /= counts[word]
标点符号=r''!“#$%&'()*+,-./:@[\]^_`{|}~'''
def删除标点符号(字符串):
新字符串=[]
对于字符串中的字符:
如果字符不在标点符号中:
新字符串。追加(字符)
返回“”。加入(新字符串)
分数={}
计数={}
以open('text.txt','r')作为数据:
lines=data.readlines()
对于行中的行:
行=删除标点符号(行)
words=line.split()
#从第行删除第一个“单词”,该行是数字评级
分数=整数(words.pop(0))
用文字表示:
如果单词在分数中:
分数[单词]+=分数
计数[字]+=1
其他:
分数
计数[字]=1
对于分数中的单词:
分数[单词]/=计数[单词]
这可以让你在字典里找到分数和唯一单词的计数(只需打印分数和计数就可以了)。让我知道它是否有效,因为我无法真正测试它,因为我没有您正在使用的文本文件。

尝试以下方法:

PUNCTUATION = r'''!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~'''


def remove_punctuation(string):
    new_string = []
    for character in string:
        if character not in PUNCTUATION:
            new_string.append(character)

    return "".join(new_string)


scores = {}
counts = {}


with open('text.txt', 'r') as data:
    lines = data.readlines()


for line in lines:
    line = remove_punctuation(line)

    words = line.split()

    # Removes first 'word' from line which is the number rating
    score = int(words.pop(0))

    for word in words:
        if word in scores:
            scores[word] += score
            counts[word] += 1
        else:
            scores[word] = score
            counts[word] = 1

for word in scores:
    scores[word] /= counts[word]
标点符号=r'''!"#$%&'()*+,-./:;?@[\]^_`{|}~'''
def删除标点符号(字符串):
新字符串=[]
对于字符串中的字符:
如果字符不在标点符号中:
新字符串。追加(字符)
返回“”。加入(新字符串)
分数={}
计数={}
以open('text.txt','r')作为数据:
lines=data.readlines()
对于行中的行:
行=删除标点符号(行)
words=line.split()
#从第行删除第一个“单词”,该行是数字评级
分数=整数(words.pop(0))
用文字表示:
如果单词在分数中:
分数[单词]+=分数
计数[字]+=1
其他:
分数
计数[字]=1
对于分数中的单词:
分数[单词]/=计数[单词]
这将为您提供带有分数和唯一单词计数的词典(只需打印分数和计数即可查看)。请告诉我它是否有效,因为我没有您使用的文本文件,因此无法真正测试它