基本上,我有这个清单。[4097, add, 1, divide, blank, subtract, 3, floor, 1, add, 2, multiply, 8]
通过这个列表,我试图将[blank, subtract, 3]
转换为[-3]
。
它将负数解释为具有带空左操作数的减法符号的等式。
有没有办法将合并[blank, subtract, 3]
组合成[-3]
?或者将[blank, add, 3]
转换为[3]
?
基本上,我有这个清单。[4097, add, 1, divide, blank, subtract, 3, floor, 1, add, 2, multiply, 8]
通过这个列表,我试图将[blank, subtract, 3]
转换为[-3]
。
它将负数解释为具有带空左操作数的减法符号的等式。
有没有办法将合并[blank, subtract, 3]
组合成[-3]
?或者将[blank, add, 3]
转换为[3]
?
3条答案
按热度按时间wvt8vs2t1#
您可以使用循环遍历列表,然后检查特定的关键字。
例如:
blank
代表什么,我用它作为表达式之间的分隔符。输出:
编辑:
如果blank没有做任何事情,你可以简单地忽略它并继续前进:
输出:
btqmn9zl2#
你将需要通过列表来删除你不想要的东西,并添加你想要的东西,没有自动的方法将三个项目替换为一个,但可以通过一个简单的函数来完成。
4097“,”加“,”1“,”除“,”空“,”减“,”3“,”地板“,”1“,”加“,”2“,”乘“,”8“] print(replace_blank(list_bla))
由@nokla提出的编辑。这个函数的空间复杂度是O(1)。
0kjbasz63#
在处理这些示例时,我们假设: