Getting indices of True values in a boolean list

前端 未结 7 1915
不思量自难忘°
不思量自难忘° 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:25

    If you have numpy available:

    >>> import numpy as np
    >>> states = [False, False, False, False, True, True, False, True, False, False, False, False, False, False, False, False]
    >>> np.where(states)[0]
    array([4, 5, 7])
    
    0 讨论(0)
提交回复
热议问题