gpt4 book ai didi

python - 从子字符串中提取的映射运算符

转载 作者:行者123 更新时间:2023-11-28 22:15:49 28 4
gpt4 key购买 nike

我有字典列表:

print (L)
[{0: 'x==1', 1: 'y==2', 2: 'z!=1'}, {0: 'x==1', 1: 'y<=3', 2: 'z>1'}]

我想创建元组,其值在运算符之前、运算符和之后的值:

#first step
wanted = [[('x', '==', '1'), ('y', '==', '2'), ('z', '!=', '1')],
[('x', '==', '1'), ('y', '<=', '3'), ('z', '>', '1')]]

然后通过运算符映射第二个值:

import operator

ops = {'>': operator.gt,
'<': operator.lt,
'>=': operator.ge,
'<=': operator.le,
'==': operator.eq,
'!=': operator.ne}

#expected final output
wanted = [[('x', <built-in function eq>, '1'),
('y', <built-in function eq>, '2'),
('z', <built-in function ne>, '1')],
[('x', <built-in function eq>, '1'),
('y', <built-in function le>, '3'),
('z', <built-in function gt>, '1')]]

我尝试:

L = [[re.findall(r'(.*)([<>=!]+)(.*)', v)[0] for k, v in x.items()] for x in L]
print (L)
[[('x=', '=', '1'), ('y=', '=', '2'), ('z!', '=', '1')],
[('x=', '=', '1'), ('y<', '=', '3'), ('z', '>', '1')]]

L = [[ops[y[1]] for y in x] for x in L]

但问题是错误匹配的中间子串 - 运算符,然后错误匹配运算符的值。

正确匹配的正确正则表达式是什么?或者这是另一种可能的解决方案。例如通过 string.partition ?我开放所有可能的解决方案。

最佳答案

如果您的输入确实如此简单,我认为最直接的方法是在运算符字符上拆分:

In [1]: import re

In [2]: data = [{0: 'x==1', 1: 'y==2', 2: 'z!=1'}, {0: 'x==1', 1: 'y<=3', 2: 'z>1'}]

In [3]: rgx = re.compile(r'([<>=!]+)')

In [4]: [[rgx.split(v) for v in d.values()] for d in data]
Out[4]:
[[['x', '==', '1'], ['y', '==', '2'], ['z', '!=', '1']],
[['x', '==', '1'], ['y', '<=', '3'], ['z', '>', '1']]]

请注意,如果您将捕获组添加到拆分器正则表达式,它会被包含在内!

然后,完成它:

In [11]: ops = {'>': operator.gt,
...: '<': operator.lt,
...: '>=': operator.ge,
...: '<=': operator.le,
...: '==': operator.eq,
...: '!=': operator.ne}
...:

In [12]: parsed = [[rgx.split(v) for v in d.values()] for d in data]

In [13]: [[(x, ops[op], y) for x,op,y in ps] for ps in parsed]
Out[13]:
[[('x', <function _operator.eq>, '1'),
('y', <function _operator.eq>, '2'),
('z', <function _operator.ne>, '1')],
[('x', <function _operator.eq>, '1'),
('y', <function _operator.le>, '3'),
('z', <function _operator.gt>, '1')]]

关于python - 从子字符串中提取的映射运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52620865/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com