gpt4 book ai didi

Python无法分配给文字

转载 作者:太空宇宙 更新时间:2023-11-04 08:58:57 24 4
gpt4 key购买 nike

我是 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)

谢谢你帮忙

最佳答案

换行

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

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

除非 1 是表示长度的 l——这是单字母变量名问题的一个很好的例子。

理由

1 不是 Python 中变量的有效名称,解释器无法分配给 wordlens 中的任何值。相反,解释器会自动将 1 视为文字数字类型——即数字 1。

关于Python无法分配给文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27497196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com