If you want to use the pure pyrase method, this seems to be correct:
from pyparsing import * # lambda to define expressions def makeExpr(ch): expr = Literal(ch).setResultsName(ch, listAllMatches=True) return expr expr = OneOrMore(MatchFirst(makeExpr(c) for c in "abc")) expr.setParseAction(lambda tokens: [[a,len(b)] for a,b in tokens.items()]) tests = """\ abc bbbc cccaa """.splitlines() for t in tests: print t,expr.parseString(t).asList()
Print
abc [['a', 1], ['c', 1], ['b', 1]] bbbc [['c', 1], ['b', 3]] cccaa [['a', 2], ['c', 3]]
But this starts to penetrate into the obscure area of ββthe code, as it relies on some of the more mysterious features of pyparsing. In general, I like the frequency counters that use defaultdict (have not tried Counter yet), since it is pretty clear what you are doing.
Paulmcg
source share