Python街道islower()方法在我的代码中似乎不起作用?

2024-09-29 06:31:35 发布

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

我正在通过古腾堡项目网站分析《麦克白》的文本,并试图通过提及人物的名字来创建一个人物列表。我知道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”等。我现在的代码有什么遗漏吗?你知道吗


Tags: 代码in文本列表forstringrequestssentence