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.
# Python3 code to demonstrate # Contiguous Boolean Ranging # using enumerate() + zip() + list comprehension # initializing list test_list = [True, True, False, False, True, True, True, True, False, True] # printing the original list print ("The original list is : " + str(test_list)) # using enumerate() + zip() + list comprehension # for Contiguous Boolean Ranging res = [x for x, (i , j) in enumerate(zip( [2] + test_list, test_list + [2])) if i != j] # printing result print ("The boolean range list is : " + str(res)) |
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.
# Python3 code to demonstrate # Contiguous Boolean Ranging # using sum() + accumulate() + groupby() from itertools import accumulate, groupby # initializing list test_list = [True, True, False, False, False, True, True, True, False, False] # printing the original list print ("The original list is : " + str(test_list)) # using sum() + accumulate() + groupby() # for Contiguous Boolean Ranging res = [0] + list(accumulate(sum(1 for i in j) for i, j in groupby(test_list))) # printing result print ("The boolean range list is : " + str(res)) |
The original list is : [True, True, False, False, False, True, True, True, False, False]
The boolean range list is : [0, 2, 5, 8, 10]
Recommended Posts:
- Python | Boolean list initialization
- Python | Ways to concatenate boolean to string
- Python Program for Largest Sum Contiguous Subarray
- Python | Ways to convert Boolean values to integer
- Python | Print unique rows in a given boolean matrix using Set with tuples
- Python program to fetch the indices of true values in a Boolean list
- Python range() function
- range() to a list in Python
- range() vs xrange() in Python
- Python | range() method
- Python | Delete elements in range
- Python | Numbers in a list within a given range
- Python | range() does not return an iterator
- Python | Remove all sublists outside the given range
- Python | Count set bits in a range
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



