假设我有一个动作列表,它可以包含三种不同类型的动作:
A型:可以包含所有类型的动作(分离)
B型:可以包含所有类型的操作(有序连接)
C型:不能包含子动作。这是我最后想要的水平。
我想(基于:python - representing boolean expressions with lists)分离和连接可以分别用元组表示,但我不确定这是否是最佳解决方案。
对于类型A和B,有一个包含类型元素的字典,例如
type_a = {
‘a1’: ('b1', 'a2'),
‘a2’: ('c1', 'c2')
}
type_b = {
‘b1’: ['c4', 'c5', 'c7'],
‘b2’:['c3', 'c4']
}
详细说明:
'a1'等于('b1', 'a2')
,等于(['c4', 'c5','c7'], 'c1', 'c2')
'a2'等于('c1', 'c2')
'b1'等于['c4', 'c5', 'c7']
'b2'等于['c3', 'c4']
示例输入:
['a1', 'b2', 'c6']
预期输出:
结果应该只包含C类动作。
原始
[(['c4', 'c5', 'c7'], 'c1', 'c2'), 'c3', 'c4', 'c6']
所有组合
['c4', 'c5','c7', 'c3', 'c4', 'c6']
['c1', 'c3', 'c4', 'c6']
['c2', 'c3', 'c4', 'c6']
问题:
感谢您的帮助。
答案 0 :(得分:1)
def combinations(actions):
if len(actions)==1:
action= actions[0]
try:
actions= type_a[action]
except KeyError:
try:
actions= type_b[action]
except KeyError:
#action is of type C, the only possible combination is itself
yield actions
else:
#action is of type B (conjunction), combine all the actions
for combination in combinations(actions):
yield combination
else:
#action is of type A (disjunction), generate combinations for each action
for action in actions:
for combination in combinations([action]):
yield combination
else:
#generate combinations for the first action in the list
#and combine them with the combinations for the rest of the list
action= actions[0]
for combination in combinations(actions[1:]):
for combo in combinations([action]):
yield combo + combination
我们的想法是为第一个动作('a1'
)生成所有可能的值,并将它们与剩余动作(['b2', 'c6']
)的(递归生成的)组合结合起来。
这也消除了表示连接和与列表和元组分离的需要,说实话,我觉得相当混乱。
答案 1 :(得分:0)
Python中还有一个set type支持集合操作 - 如果你不关心订购。