在寻找了很多没有成功的东西之后,我需要帮助。在
我有一个元组列表。列表列表中的每个列表都代表我系统中特定数量的公式。此列表中的任何元素都是表示元素类型(变量、参数、常量、操作…)和元素名称的元组。例如,对于公式x1+x2+A1,x1-x3和sin(x2)+A1我们将得到:
[
[('VAR', 'x1'), ('PLUS', '+'), ('VAR', 'x2'), ('PLUS', '+'), ('PAR', 'A1')],
[('VAR', 'x1'), ('LESS', '-'), ('VAR', 'x3')],
[('SIN', 'sin'), ('VAR', 'x2'), ('PLUS', '+'), ('PAR', 'A1')]
]
我试图确定每个变量出现在哪个公式中。在上面的例子中,x1变量在1和2公式上,x2变量在1和3公式上,x3在2公式中,所以我的输出如下:
^{pr2}$目前,我有一些非常低效的代码根本不起作用,但这里是:
cont = 0
for subL1 in L:
for subL2 in L:
if len(subL1) != 1 and len(subL2) != 1:
if subL1 != subL2 and subL2:
for x,y in subL1:
for z,t in subL2:
if ( x == 'VAR'
and z == 'VAR'
and y == t
):
print "Variable", y , "repeated"
else:
print "list with 1 lenght\n"
subL1.pop(0)
cont = cont + 1
可以使用^{} 为每个变量存储公式(实际上是列表列表中的索引):
例如:
^{pr2}$它提供:
要获得所需的输出,只需再次将其转换为列表,例如(仅限于python-3.x):
相关问题 更多 >
编程相关推荐