setResultsName仅显示第一个列表项

时间:2012-04-29 08:50:26

标签: python python-2.x pyparsing

考虑以下最小例子:

from pyparsing import Word, delimitedList
the_list = delimitedList(Word("fine").setResultsName("extension", listAllMatches=True))
prefixed = Word("okay").setResultsName("base") + the_list
prefixed.addParseAction(lambda x: map(lambda element: x.base + element, x.extension))
final = prefixed.setResultsName("doesNotWork", listAllMatches=True) + Word("x")

final.parseString("ookf,i,n,ex")

返回

(['ookf', 'ooki', 'ookn', 'ooke', 'x'], {'doesNotWork': [((['ookf'], {}), 0)]})

如何让pyparsing将整个列表,['ookf','ooki','ookn','ooke','x']分配给doesNotWork,而不仅仅是第一个列表项?

1 个答案:

答案 0 :(得分:2)

如果您将prefixed更改为:

,该怎么办?
prefixed = Group(Word("okay").setResultsName("base") + the_list)

这可以接受吗?