One-liner funcional abominável com sum
e map
:
sum(map(lambda x: 1 if x.startswith(tuple(pref)) else 0, words))
Abominável one-liner com reduce
:
reduce(lambda x, y: x + 1 if y.startswith(tuple(pref)) else x, words, 0)
:)
#Update:
Update:
Conforme requisitos do OP, ainda mais abominável one-liner das neves:
map(lambda p: reduce(lambda c, w: c + 1 if w.startswith(p) else c, words, 0), pref)
Exemplo menos forçado:
def countPrefix(words, prefix):
return len([1 for w in words if w.startswith(prefix)])
[countPrefix(words, p) for p in pref]
Resultado:
[2, 2, 6]