简体   繁体   English

pyparsing不是嵌套列表......为什么?

[英]pyparsing isn't nesting list … why?

For some reason, pyparsing isn't nesting the list for my string: 出于某种原因,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']

I'm not sure why the pyparsing isn't creating lists around 22+, AKo-ATo, and KQz (or any layers deeper than that). 我不确定为什么pyparsing不会创建22 +,AKo-ATo和KQz(或任何比这更深的层)的列表。 What am I missing? 我错过了什么?

Pyparsing isn't grouping these tokens because you didn't tell it to. Pyparsing不会对这些令牌进行分组,因为您没有告诉它。 Pyparsing's default behavior is to simply string together all matched tokens into a single list. Pyparsing的默认行为是将所有匹配的标记简单地串在一起。 To get grouping of your tokens, wrap the expressions in your parser that are to be grouped in a pyparsing Group expression. 要对令牌进行分组,请将解析器中的表达式包装在一个pyparsing Group表达式中。 In your case, change series from: 在您的情况下,更改series

series = hand + Optional(greater | through + hand)

to

series = Group(hand + Optional(greater | through + hand))

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 : 另外,我建议您不要像在series中那样实现自己的逗号分隔列表,而是使用pyparsing helper, delimitedList

hand_range = delimitedList(series)

delimitedList assumes comma delimiters, but any character (or even complete pyparsing expression) can be given as the delim argument. delimitedList采用逗号分隔符,但任何字符(甚至完整的pyparsing表达式)都可以作为delim参数给出。 The delimiters themselves are suppressed from the results, as delimitedList assumes that the delimiters are there simply as separators between the important bits, the list elements. 分隔符本身从结果中被抑制,因为delimitedList假定分隔符仅仅是重要位,列表元素之间的分隔符。

After making these two changes, the parse results now start to look more like what you are asking for: 完成这两项更改后,解析结果现在开始看起来更像您要求的内容:

[['2', '2', '+'], ['A', 'K', 'o', '-', 'A', 'T', 'o'], ['K', 'Q', 'z']]

I'm guessing that you might also want to put Group around the hand definition, to structure those results as well. 我猜你也可能想把Group围绕hand定义,以构建那些结果。

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. 如果这是一个将以某种方式进行评估的表达式(如扑克手),那么请查看pyparsing wiki上的这些示例,它们使用类作为解析操作来构造可以评估rank或boolean值的对象或者其他。

http://pyparsing.wikispaces.com/file/view/invRegex.py http://pyparsing.wikispaces.com/file/view/invRegex.py

http://pyparsing.wikispaces.com/file/view/simpleBool.py http://pyparsing.wikispaces.com/file/view/simpleBool.py

http://pyparsing.wikispaces.com/file/view/eval_arith.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 . 如果为这些表达式构造对象,则不需要使用Group

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM