从名称列表中,我想排除具有 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"
改变
if "Contae" not in irish[h]:
到
if "Contae" not in h: