Getting indices of True values in a boolean list

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

    Simply do this:

    def which_index(self):
        return [
            i for i in range(len(self.states))
            if self.states[i] == True
        ]
    

提交回复
热议问题