“TypeError:list indices must be integers or slices,not NavigableString”in python [duplicate]

kg7wmglp  于 2024-01-05  发布在  Python
关注(0)|答案(1)|浏览(107)

此问题在此处已有答案

TypeError: list indices must be integers or slices, not str, Python [duplicate](1个答案)
昨天就关门了。
我想从名称列表中排除名称为两个单词的元素。
以下是原始列表:

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

字符串
我想从上面的列表中排除那些包含“Contae”的元素。所以我想创建一个新的更小的列表,它不包含那些元素。
我是这样做的:

  1. irish=native[0:]
  2. irish_names=[]
  3. for h in irish:
  4. if "Contae" not in irish[h]:
  5. irish_names.append(h)
  6. print(irish_names)


但是我得到了以下错误:

  1. " if "Contae" not in irish[h]:
  2. ~~~~~^^^
  3. TypeError: list indices must be integers or slices, not NavigableString"

mitkmikd

mitkmikd1#

变化
第一个月

if "Contae" not in h:

相关问题