Python | Contiguous Boolean Range
Sometimes, we come across a problem in which we have a lot of data in a list in the form of True and False value, this kind of problem is common in Machine learning domain and sometimes we need to know that at a particular position which boolean value was present. Let’s discuss certain ways in which this can be done.
Method #1 : Using
enumerate() + zip() + list comprehension
By using combination of above three functions, this task can easily be accomplished. The enumerate function handles the role of iteration, zip function groups the like values and the logic part is handled by list comprehension.
The original list is : [True, True, False, False, True, True, True, True, False, True]
The boolean range list is : [0, 2, 4, 8, 9, 10]
Method #2 : Using
sum() + accumulate() + groupby()
The above three functions can also be clubbed together to achieve the particular task, as they use the iterators to achieve it. The sum function counts each value, groupby groups each one and all together are accumulated by the accumulate function.
The original list is : [True, True, False, False, False, True, True, True, False, False]
The boolean range list is : [0, 2, 5, 8, 10]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course