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

发布于 2024-10-03 03:08:16 字数 613 浏览 6 评论 0原文

由于某种原因,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随心而道 2024-10-10 03:08:16

Pyparsing 不会对这些标记进行分组,因为您没有告诉它这样做。 Pyparsing 的默认行为是简单地将所有匹配的标记串到一个列表中。要对标记进行分组,请将解析器中的表达式包装在 pyparsing Group 表达式中进行分组。在您的情况下,将 series 从: 更改

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

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

另外,我建议您不要像在 series 中那样实现自己的逗号分隔列表,而是使用 pyparsing 助手, delimitedList:

hand_range = delimitedList(series)

delimitedList 假定逗号分隔符,但任何字符(甚至完整的 pyparsing 表达式)都可以作为 delim 参数给出。分隔符本身在结果中被抑制,因为 delimitedList 假设分隔符只是作为重要位(列表元素)之间的分隔符。

进行这两项更改后,解析结果现在开始看起来更像您所要求的:

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

我猜您可能还想将 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, change series from:

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:

hand_range = delimitedList(series)

delimitedList assumes comma delimiters, but any character (or even complete pyparsing expression) can be given as the delim argument. 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.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文