第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將字符串分解為嵌套令牌?

如何將字符串分解為嵌套令牌?

慕斯王 2022-09-20 15:15:51
我有由布爾項和方程組成的字符串,如下所示x=1 AND (x=2 OR x=3) AND NOT (x=4 AND x=5) AND (x=5) AND y=1我想將 它們分成由 分隔的組,同時將括號作為分組運算符。例如,上面字符串的結果將是xAND[['x=1'], ['x=2', 'x=3'], ['x=4'], ['x=5'], ['x=5']]x=2并且位于同一組中,因為它們由 分組,而不是由 分隔。最后一個等式被忽略,因為它不以 開頭。x=3()ANDx更新另一個例子是x=1 AND (x=2 OR (x=3 AND x=4))其中每個等式應該是分開的[['x=1'], ['x=2', [['x=3'], ['x=4']]]我發(fā)現(xiàn)最接近的是這篇文章,但我不知道如何根據(jù)我的需求修改它。
查看完整描述

2 回答

?
BIG陽

TA貢獻1859條經(jīng)驗 獲得超6個贊

正如您可能在另一個問題中看到的那樣,解析諸如此類的中綴表示法最好使用幫助程序(以前稱為 )進行解析。以下是用于您的問題的基礎知識:infixNotationoperatorPrecedenceinfixNotation


import pyparsing as pp


# define expressions for boolean operator keywords, and for an ident

# (which we take care not to parse an operator as an identifier)

AND, OR, NOT = map(pp.Keyword, "AND OR NOT".split())

any_keyword = AND | OR | NOT

ident = pp.ungroup(~any_keyword + pp.Char(pp.alphas))

ident.setName("ident")


# use pyparsing_common.number pre-defined expression for any numeric value

numeric_value = pp.pyparsing_common.number


# define an expression for 'x=1', 'y!=200', etc.

comparison_op = pp.oneOf("= != < > <= >=")

comparison = pp.Group(ident + comparison_op + numeric_value)

comparison.setName("comparison")


# define classes for the parsed results, where we can do further processing by

# node type later

class Node:

    oper = None

    def __init__(self, tokens):

        self.tokens = tokens[0]


    def __repr__(self):

        return "{}:{!r}".format(self.oper, self.tokens.asList())


class UnaryNode(Node):

    def __init__(self, tokens):

        super().__init__(tokens)

        del self.tokens[0]


class BinaryNode(Node):

    def __init__(self, tokens):

        super().__init__(tokens)

        del self.tokens[1::2]


class NotNode(UnaryNode):

    oper = "NOT"


class AndNode(BinaryNode):

    oper = "AND"


class OrNode(BinaryNode):

    oper = "OR"


# use infixNotation helper to define recursive expression parser,

# including handling of nesting in parentheses

expr = pp.infixNotation(comparison,

        [

            (NOT, 1, pp.opAssoc.RIGHT, NotNode),

            (AND, 2, pp.opAssoc.LEFT, AndNode),

            (OR, 2, pp.opAssoc.LEFT, OrNode),

        ])

現(xiàn)在嘗試在測試字符串上使用此解析器。expr


test = "x=1 AND (x=2 OR x=3 OR y=12) AND NOT (x=4 AND x=5) AND (x=6) AND y=7"


try:

    result = expr.parseString(test, parseAll=True)

    print(test)

    print(result)

except pp.ParseException as pe:

    print(pp.ParseException.explain(pe))

給:


x=1 AND (x=2 OR x=3 OR y=12) AND NOT (x=4 AND x=5) AND (x=6) AND y=7

[AND:[['x', '=', 1], OR:[['x', '=', 2], ['x', '=', 3], ['y', '=', 12]], NOT:[AND:[['x', '=', 4], ['x', '=', 5]]], ['x', '=', 6], ['y', '=', 7]]]

從這一點開始,折疊嵌套的 AND 節(jié)點并刪除非比較可以使用常規(guī) Python 完成。x


查看完整回答
反對 回復 2022-09-20
?
慕雪6442864

TA貢獻1812條經(jīng)驗 獲得超5個贊

我想你可以做這樣的事情:


operators = ["AND NOT", "AND"]

sepChar = ":"

yourInputString = yourInputString.replace("(","").replace(")","") # remove the parenthesis


# Replace your operators with the separator character

for op in operators:

    yourInputString = yourInputString.replace(op,sepChar)


# output of your string so far

# yourInputString

# 'x=1 : x=2 OR x=3 : x=4 : x=5 : x=5 : y=1'


# Create a list with the separator character

operationsList = youtInputString.split(sepChar) 


# operationsList

# ['x=1', 'x=2 OR x=3', 'x=4', 'x=5', 'x=5', 'y=1']


# For the second result, let's do another operation list:

operators2 = ["OR"]

output = []


# Loop to find the other operators

for op in operationsList:

    for operator in operators2:

        if operator in op:

            op = op.split(operator)

    output.append(op)


# output:

# [['x=1'], ['x=2', 'x=3'], ['x=4'], ['x=5'], ['x=5'],['y=1']]

        

在這種情況下,我使用“:”作為分隔字符,但您可以根據(jù)需要進行更改。如果有幫助,請讓我知道!


編輯


對于括號嵌套方法,我?guī)砹艘恍┚实臇|西:


import re

operators = ["AND NOT","AND","OR"]


# Substitute parenthesis

yourInputString = yourInputString.replace("(","[").replace(")","]")


# yourInputString

# "[x=1 AND [x=2 OR x=3] AND NOT [x=4 AND x=5] AND [x=5] AND y=1]"


# Replace your operators

for op in operators:

    yourInputString = yourInputString(op,",")


# yourInputString

# "[x=1 , [x=2 , x=3] , [x=4 , x=5] , [x=5] , y=1]"


# Find matches like x = 5 and substitue with 'x = 5'

compiler = re.compile(r"[xyz]{1}=\d")

matches = compiler.findall(yourInputString)


# matches

# ['x=1', 'x=2', 'x=3', 'x=4', 'x=5', 'x=5', 'y=1']


# Convert the list into unique outputs

matches = list(set(matches))


# matches

# ['x=1', 'x=2', 'x=3', 'x=4', 'x=5', 'y=1']


# Replace your matches to add quotes to each element

for match in matches:

    yourInputString = yourInputString.replace(match,f"'{match}'")



# yourInputString

# "['x=1' , ['x=2' , 'x=3'] , ['x=4' , 'x=5'] , ['x=5'] , 'y=1']"


# Here is the special move, convert your text into list

myList = eval(yourInputString)


# myList

# ['x=1', ['x=2', 'x=3'], ['x=4', 'x=5'], ['x=5'], 'y=1']


查看完整回答
反對 回復 2022-09-20
  • 2 回答
  • 0 關注
  • 105 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號