Getting indices of True values in a boolean list

前端 未结 7 1916
不思量自难忘°
不思量自难忘° 2020-12-07 11:52

I have a piece of my code where I\'m supposed to create a switchboard. I want to return a list of all the switches that are on. Here \"on\" will equal True and

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-07 12:05

    Using element-wise multiplication and a set:

    >>> states = [False, False, False, False, True, True, False, True, False, False, False, False, False, False, False, False]
    >>> set(multiply(states,range(1,len(states)+1))-1).difference({-1})
    

    Output: {4, 5, 7}

提交回复
热议问题