首页 > 解决方案 > 打印列表中所有目标出现的索引的函数

问题描述

我被困在一个问题上:

编写一个函数,打印项目中所有出现的print_matching_indexes(items, target)索引。我们使用 for 循环来迭代列表targetlist

我已经写了:

def print_matching_indexes(items, target):
    """Prints the indexes of all (target) in the list (items)"""
    for i in items:
        print (items.index)

但我收到消息:

<built-in method index of list object at 0x7fc8103caa00>
<built-in method index of list object at 0x7fc8103caa00>
<built-in method index of list object at 0x7fc8103caa00>

如果它有效,则以下测试代码应导致:

pets = ['dog', 'cat', 'fish', 'cat', 'dog', 'iguana']
print_matching_indexes(pets, 'cat')

结果

1
3

我要去哪里错的任何指示?

标签: python

解决方案


推荐阅读