Getting all the min elements and its indices from a list

前端 未结 2 893
执笔经年
执笔经年 2020-12-17 01:19

I have a list that has a minimum element that is present multiple times like

a = [1,2,1,1,4,5,6]

And I want Python to return the element

2条回答
  •  心在旅途
    2020-12-17 01:40

    I'd just do it like this:

    minimum = min(a)
    indices = [i for i, v in enumerate(a) if v == minimum]
    

提交回复
热议问题