Skip to main content
Commonmark migration
Fonte Link

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]

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:

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]

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:

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]
adicionou 398 caracteres ao conteúdo
Fonte Link
Anthony Accioly
  • 21,2mil
  • 2
  • 54
  • 109

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:

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]

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)

:)

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:

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]
Fonte Link
Anthony Accioly
  • 21,2mil
  • 2
  • 54
  • 109

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)

:)