Python shortcut for len (list (filter (lambda x: criteria, iterable)))

Imagine you need to count the number of iterable elements that satisfy some criteria - is there a better way to do this? This is python3, of course. But 2 is excellent. I cannot find anything suitable in python collections or itertools or in embedded firmware.

+5
source share
3 answers

Try the quantify recipe from itertools recipes :

 def quantify(iterable, pred=bool): "Count how many times the predicate is true" return sum(map(pred, iterable)) 

more_itertools already implements this recipe, so it is even more compact:

 >>> import more_itertools as mit >>> iterable = [True, False, True, True] >>> mit.quantify(iterable) 3 

For comparison:

 >>> #len(list(filter(lambda x: criteria, iterable))) >>> len(list(filter(lambda x: x is True, iterable))) 3 

Performance

 # A: len(list(filter(lambda x: criteria, iterable))) >>> %timeit -n 1000000 len(list(filter(lambda i: i is True, iterable))) 1000000 loops, best of 3: 2.48 ยตs per loop # B: quantify(iterable, pred=condition) >>> %timeit -n 1000000 mit.quantify(iterable) 1000000 loops, best of 3: 1.87 ยตs per loop # C: ilen(item for item in iterable if condition) >>> %timeit -n 1000000 mit.ilen(i for i in iterable if i is True) 1000000 loops, best of 3: 5.27 ยตs per loop # D: len([item for item in iterable if condition]) >>> %timeit -n 1000000 len([i for i in iterable if i is True]) 1000000 loops, best of 3: 973 ns per loop # E: sum(1 for _ in iterable if condition) >>> %timeit -n 1000000 sum(1 for i in iterable if i is True) 1000000 loops, best of 3: 1.34 ยตs per loop 
  • A : control - author
  • B : faster - quantify , itertools recipe
  • C : the slowest is a generator expression, uses more_itertools.ilen to evaluate
  • D : quick list view.
  • E : faster - generator expression, sum(1 for _ in ...) idiom

While more_itertools.quantify is concise, the idiom generator expression is paired, if not faster. However, classic listings (first proposed by @ ๅฎๆฐ ๆŽ) are top performers.

See also thread when extending len() to generators.

+3
source
 sum(1 for i in A if condition(i)) 
+3
source

List enumerations provide a concise way to create lists. general applications should create new lists in which each element is the result of certain operations applied to each member of a different sequence or iteration or creation of a subsequence of those elements that satisfy a certain state condition.

 >>> squares = [] >>> for x in range(10): ... squares.append(x**2) ... >>> squares [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] 

Note that this creates (or overwrites) a variable named x that still exists after the loop finishes. We can calculate the list of squares without any side effects using:

 squares = list(map(lambda x: x**2, range(10))) 

or, equivalently:

 squares = [x**2 for x in range(10)] 

than just setting a condition in if status like this

 >>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y] [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)] 

you have a list of elements that match this condition and you can use len(lsit_) to count the elements

List comprehension document

+1
source

Source: https://habr.com/ru/post/1261362/


All Articles