Python不能分配给li

2024-10-03 17:20:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python的新手,正在尝试在数据集库中记录词频。 这就是我所拥有的,它告诉我它不能赋值给第20行的字面量。在

import movie_scripts
import matplotlib.pyplot as plt
all_movies = movie_scripts.get_all_movies()

romeo = (all_movies[1]['lines']['all'])

tokens = WSTokenizer().tokenize(romeo)

male_words= set(['man','men',"man's", "men's", 'mr', 'mister', 'he', "he's", 'his', 'him', 'boy',"boys", 'guy', 'guys', 'brother', 'brothers', 'father', 'fathers', 'dad', 'dads', 'grandpa', 'grandpas', 'grandfather', 'boyfriend', 'boyfriends', 'uncle', 'uncles', 'mr', 'sir', 'sirs', 'son', 'sons', 'king', 'kings', 'prince', 'princes', 'daddy', 'daddies', "daddy's", 'chairman', 'chairmen', 'counrtyman', 'countrymmen', 'doorman', 'doormen', 'waiter', 'waiters', 'stud', 'studs', 'son of a bitch', 'sons of bitches', 'bro', 'bros', 'dude', 'dudes', "dude's", 'actor', 'actors', 'god', 'gods', "god's", 'husband', 'husbands', "husband's", 'himself', 'lord', 'lords', 'knight', 'knights', 'groom', 'grooms', "groom's"])
female_words = set(['woman', 'women', 'girl', 'girls', 'she', 'ms', 'her', "she's", "her's", 'lady', 'ladies', 'bitch', "bitch's", 'bitches', 'mom', 'mother', 'moms', 'mothers', "mom's", "mother's", 'grandmom', 'grandmas', 'grandmother', 'grandmothers', 'granddaughter', 'granddaughters', 'aunt', 'aunts', "ma'am", 'madame', 'daughter', 'daughters', 'sister', 'sisters', 'queen', 'queens', 'princess', 'princesses', 'mommy', 'mommies', "mommy's", 'waitress', 'waitresses', 'babe', 'babes', 'damsel', 'damsels', 'bird', 'birds', 'girlfriend', 'girlfriends', "girlfriend's", 'actress', 'actresses', 'goddess', 'goddesses', 'gal', 'gals', 'wife', 'wives', 'herself', 'dame', 'dames', 'bride', 'brides', "bride's"])

ended_with_male_words = 0
freq_dist = FreqDist()
for token in tokens:
    if ended_in_male_words:
    freq_dist.inc(len(token.type()))
ended_with_male_words = token.type()[-1].lower() in male_words
wordlens = freq_dis.samples()
wordlens.sort()
points = [(1, freq_dist.freq(1)) for 1 in wordlens]
Plot(points)

谢谢,请帮忙


Tags: inimporttokendistscriptsallmoviesmovie
2条回答

你不能用这句话:

points = [(1, freq_dist.freq(1)) for 1 in wordlens]

因为你正在有效地将wordlens中的每一项分配给数字1。数字不能用作变量,因此出现错误。您可以使用变量来解决它,例如:

^{pr2}$

换行

points = [(1, freq_dist.freq(1)) for 1 in wordlens]

^{pr2}$

除非1本来是l,否则这是单字母变量名出现问题的一个很好的例子。在

基本原理

1不是Python中变量的有效名称,解释器无法为wordlens中的任何值赋值。{1}解释器自动将数字视为数字。在

相关问题 更多 >