Python compress Iterator

compress(data, selectors) iterator drops data based on selectors

>>> from itertools import *
>>> x = compress([1,4,6,2,34,9],[True,False,True,True,True,False])
>>> for i in x:
>>> print(i)
1
6
2
34
The data is a list, and the selector is a list with the same length. For each indice i of the lists, if selector[i] is False, then drop data[i]. Selector elements can be expressed as True and False, or 1 and 0 or other boolean equivalents.






















endmemo.com © 2024  | Terms of Use | Privacy | Home