Python 中的“类型错误:列表索引必须是整数或切片,而不是 NavigableString”

问题描述 投票:0回答:1

从名称列表中,我想排除具有 2 个单词名称的元素。

这是原始列表:

native=['Aontroim', 'Contae Aontroma', 'Ard Mhacha', 'Contae Ard Mhacha', 'Ceatharlach', 'Contae Cheatharlach', 'An Cabhán' ]

我想从上面的列表中排除那些包含“Contae”的元素。所以我想创建一个新的更小的列表,其中没有这些元素。

这就是我所做的:

irish=native[0:]
irish_names=[]
for h in irish:
    if "Contae" not in irish[h]:
        irish_names.append(h)
print(irish_names)

但是我收到以下错误:

" if "Contae" not in irish[h]:
                       ~~~~~^^^
TypeError: list indices must be integers or slices, not NavigableString"
python list data-science typeerror
1个回答
1
投票

改变

if "Contae" not in irish[h]:

if "Contae" not in h:

© www.soinside.com 2019 - 2024. All rights reserved.