pyparsing 不是嵌套列表...为什么?
由于某种原因,pyparsing 没有嵌套我的字符串的列表:
rank = oneOf("2 3 4 5 6 7 8 9 T J Q K A")
suit = oneOf("h c d s")
card = rank + Optional(suit)
suit_filter = oneOf("z o")
hand = card + card + Optional(suit_filter)
greater = Literal("+")
through = Literal("-")
series = hand + Optional(greater | through + hand)
series_split = Literal(",")
hand_range = series + ZeroOrMore(series_split + series)
hand_range.parseString('22+,AKo-ATo,KQz')
>> ['2', '2', '+', ',', 'A', 'K', 'o', '-', 'A', 'T', 'o', ',', 'K', 'Q', 'z']
我不确定为什么 pyparsing 没有创建围绕 22+、AKo-ATo 和 KQz(或任何比这更深的层)的列表。我缺少什么?
For some reason, pyparsing isn't nesting the list for my string:
rank = oneOf("2 3 4 5 6 7 8 9 T J Q K A")
suit = oneOf("h c d s")
card = rank + Optional(suit)
suit_filter = oneOf("z o")
hand = card + card + Optional(suit_filter)
greater = Literal("+")
through = Literal("-")
series = hand + Optional(greater | through + hand)
series_split = Literal(",")
hand_range = series + ZeroOrMore(series_split + series)
hand_range.parseString('22+,AKo-ATo,KQz')
>> ['2', '2', '+', ',', 'A', 'K', 'o', '-', 'A', 'T', 'o', ',', 'K', 'Q', 'z']
I'm not sure why the pyparsing isn't creating lists around 22+, AKo-ATo, and KQz (or any layers deeper than that). What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Pyparsing 不会对这些标记进行分组,因为您没有告诉它这样做。 Pyparsing 的默认行为是简单地将所有匹配的标记串到一个列表中。要对标记进行分组,请将解析器中的表达式包装在 pyparsing
Group
表达式中进行分组。在您的情况下,将series
从: 更改为
另外,我建议您不要像在
series
中那样实现自己的逗号分隔列表,而是使用 pyparsing 助手,delimitedList
:delimitedList
假定逗号分隔符,但任何字符(甚至完整的 pyparsing 表达式)都可以作为delim
参数给出。分隔符本身在结果中被抑制,因为delimitedList
假设分隔符只是作为重要位(列表元素)之间的分隔符。进行这两项更改后,解析结果现在开始看起来更像您所要求的:
我猜您可能还想将
Group
放在hand
周围定义,也构建这些结果。如果这是一个将以某种方式评估的表达式(如扑克牌),那么请查看 pyparsing wiki 上的这些示例,它们使用类作为解析操作来构造可以评估排名或布尔值或其他值的对象。
http://pyparsing.wikispaces.com/file/view/invRegex.py
http://pyparsing.wikispaces.com/file/view/simpleBool.py
http://pyparsing.wikispaces.com/file/view/eval_arith.py
如果您为这些表达式构造对象,则无需使用
Group
。Pyparsing isn't grouping these tokens because you didn't tell it to. Pyparsing's default behavior is to simply string together all matched tokens into a single list. To get grouping of your tokens, wrap the expressions in your parser that are to be grouped in a pyparsing
Group
expression. In your case, changeseries
from:to
Also, I recommend that you not implement your own comma-delimited list as you have done in
series
, but instead use the pyparsing helper,delimitedList
:delimitedList
assumes comma delimiters, but any character (or even complete pyparsing expression) can be given as thedelim
argument. The delimiters themselves are suppressed from the results, asdelimitedList
assumes that the delimiters are there simply as separators between the important bits, the list elements.After making these two changes, the parse results now start to look more like what you are asking for:
I'm guessing that you might also want to put
Group
around thehand
definition, to structure those results as well.If this is an expression that will be evaluated in some way (like a poker hand), then please look at these examples on the pyparsing wiki, which use classes as parse actions to construct objects that can be evaluated for rank or boolean value or whatever.
http://pyparsing.wikispaces.com/file/view/invRegex.py
http://pyparsing.wikispaces.com/file/view/simpleBool.py
http://pyparsing.wikispaces.com/file/view/eval_arith.py
If you construct objects for these expressions, then you won't need to use
Group
.