我已经用 S1 替换了 S,它工作正常。我不明白 S 在 sympy 中的意义是什么。
#!/usr/bin/env py
import itertools
import sys
import sympy
data = ['next_state=(D1&S1&~SE)|(~S1&~SE&D0)|(SE&SI))']
#data = ['next_state=(D1&S&~SE)|(~S&~SE&D0)|(SE&SI))']
data_1 = data[0].split(',')
com = None
for item in data_1:
if item.find('next_state=')!= -1:
item_list = item.split('=')
item_op = item_list[len(item_list) -1].lstrip('(').rstrip(')')
item_op = "(" + item_op + ")"
print item_op
expr = sympy.sympify(item_op)
temp_list = [ str(data) for data in expr.free_symbols]
temp_list.remove('D1')
#print len(temp_list), temp_list
for truth_values in sympy.cartes([0, 1], repeat=len(temp_list)):
values = dict(zip(temp_list, truth_values))
print values.items(), expr.subs(values)
if str(expr.subs(values)) =="D1":
com = sorted(values.items())
break
print "com",com