我的意见是:
['A', '&', 'B', '|', 'C', '|', '!', 'D', '^', 'E', '&', 'F']
它的操作数(A-F)和连接词的优先级取决于:
我需要对项目进行分组,以便本例中的输出为:
[[['A', '&', 'B'], '|', 'C'], '|', [[['!', 'D'], '^', ['E', '&', 'F']]]]
所以第一个一元否定(!)已分组。然后是二元连词(&;),然后是异或(^),最后是分离词(|)。你知道吗
有没有一种简洁的方法可以用Python实现这一点?你知道吗
示例代码,应该可以完成工作,但不太整洁:
def grp(lst, operator='|'):
i, l, r = 0, len(lst), []
while i < l:
item = lst[i]
if item == operator:
if item == '!':
r.append([item, lst[i+1]])
else:
r[-1] = [r[-1], item, lst[i+1]]
i+=1
else:
r.append(item)
i+=1
return r
lst = ['A', '&', 'B', '|', 'C', '|', '!', 'D', '^', 'E', '&', 'F']
print(grp(grp(grp(grp(lst, '!'), '&'), '^'), '|'))
# -> [[[['A', '&', 'B'], '|', 'C'], '|', [['!', 'D'], '^', ['E', '&', 'F']]]]
此解决方案概括了
grp
函数的思想,并且由于其递归结构,它只需要一次调用:请注意,parse函数本身并不知道关于二进制运算符的任何信息(除了为方便起见在此处添加的默认参数)。二元运算符及其优先级可以由第二个参数任意指定。在二进制运算符最后一次出现时拆分已解析的标记使分组左关联。(在第一次出现时拆分已解析的标记将使分组权限具有关联性,这不是通常的默认值,并且对于非交换运算符会产生意外的结果。)
我认为这是半整洁的。它又短又简单。 (虽然效率不高)
编辑:使用
rindex
,由@coproc提供。你知道吗相关问题 更多 >
编程相关推荐