3 ms·
It is summing but being used for counting (in imitation of the same style from APL) via punning on True/False as 1/0. Not what actually happens but conceptuall
by Jtsummers 3y ago
It is summing but being used for counting (in imitation of the same style from APL) via punning on True/False as 1/0.
Not what actually happens but conceptually:
ages = [17, 13, 18, 30, 12]
sum(age > 17 for age in ages)
=> sum([False, False, True, True, False])
=> sum([0, 0, 1, 1, 0])
=> 2 # via conventional summing
Since True and False are 1 and 0 for arithmetic in Python, this is just a regular sum which also happens to produce a count.
- naijaboiler 3y agoyeah if i ready the line using "sum", I would be expecting the result 48 (18+30) not 2
- zuminator 3y agoUsing the same format, sum((age > 17)*age for age in ages), perhaps.