Finding the index of an item in a list

后端 未结 30 3262
你的背包
你的背包 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:49

    All indexes with the zip function:

    get_indexes = lambda x, xs: [i for (y, i) in zip(xs, range(len(xs))) if x == y]
    
    print get_indexes(2, [1, 2, 3, 4, 5, 6, 3, 2, 3, 2])
    print get_indexes('f', 'xsfhhttytffsafweef')
    

提交回复
热议问题