此问题在此处已有答案:
TypeError: list indices must be integers or slices, not str, Python [duplicate](1个答案)
昨天就关门了。
我想从名称列表中排除名称为两个单词的元素。
以下是原始列表:
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"
型
1条答案
按热度按时间mitkmikd1#
变化
第一个月
到
if "Contae" not in h:
个