我正在通过古腾堡项目网站分析《麦克白》的文本,并试图通过提及人物的名字来创建一个人物列表。我知道nltk有办法做到这一点,但我正在努力避免这一点。我是通过在文本中找到所有“Enter”的实例来获取名称的,然后尝试删除所有小写单词。这是我目前掌握的代码:
import requests
macbeth = requests.get('http://www.gutenberg.org/cache/epub/2264/pg2264.txt').text
macbeth = macbeth.split('.')
character_list = [sentence.split() for sentence in macbeth if 'Enter' in sentence]
for sublist in character_list:
for string in sublist:
if string.islower() == True:
sublist.remove(string)
下面是打印结果时得到的输出的摘录:
[['Enter', 'Witches'],
['Enter',
'King,',
'Malcome,',
'Donalbaine,',
'Lenox,',
'attendants,',
'a',
'Captaine'],
['Enter', 'Rosse', 'Angus'],
['Enter', 'three', 'Witches'],
['Enter', 'Macbeth', 'Banquo'],
["Toth'", 'tune', 'words:', 'here?', 'Enter', 'Rosse', 'Angus']
etc.
我很难理解为什么没有从每个子列表中删除“attendars”、“a”、“three”、“tune”等。我现在的代码有什么遗漏吗?你知道吗
如果在一个for循环中从列表中删除一项,列表也会发生更改。所以在这个
for string in sublist
中,字符串不会按原始子列表的顺序循环。你知道吗相关问题 更多 >
编程相关推荐