Finding the index of an item in a list

后端 未结 30 3343
你的背包
你的背包 2020-11-21 05:28

Given a list [\"foo\", \"bar\", \"baz\"] and an item in the list \"bar\", how do I get its index (1) in Python?

30条回答
  •  渐次进展
    2020-11-21 05:59

    There is a more functional answer to this.

    list(filter(lambda x: x[1]=="bar",enumerate(["foo", "bar", "baz", "bar", "baz", "bar", "a", "b", "c"])))
    

    More generic form:

    def get_index_of(lst, element):
        return list(map(lambda x: x[0],\
           (list(filter(lambda x: x[1]==element, enumerate(lst))))))
    

提交回复
热议问题