文字が任意の順序である可能性のあるテキストから、文字の種類と数を引き出すことができるようにしたいと考えています。私が取り組んでいる他の解析が行われていますが、このビットには困惑しています!
input -> result
"abc" -> [['a',1], ['b',1],['c',1]]
"bbbc" -> [['b',3],['c',1]]
"cccaa" -> [['a',2],['c',3]]
検索またはスキャンを使用して、可能な文字ごとに繰り返すことができますが、それを行うクリーンな方法はありますか?
これは私が得た限りです:
from pyparsing import *
def handleStuff(string, location, tokens):
return [tokens[0][0], len(tokens[0])]
stype = Word("abc").setParseAction(handleStuff)
section = ZeroOrMore(stype("stype"))
print section.parseString("abc").dump()
print section.parseString("aabcc").dump()
print section.parseString("bbaaa").dump()