qq_遁去的一_1
这不是最干净的方法,但这就是我的意思:from itertools import permutationsa = [3,1,5]b = [2,4]def a_order_is_same(perm): i3, i1, i5 = perm.index(3), perm.index(1), perm.index(5) return i3 < i1 < i5ans = [p for p in permutations(a+b) if a_order_is_same(p)]for p in ans: print(p)--------------------------------------------------(3, 1, 5, 2, 4)(3, 1, 5, 4, 2)(3, 1, 2, 5, 4)(3, 1, 2, 4, 5)(3, 1, 4, 5, 2)(3, 1, 4, 2, 5)(3, 2, 1, 5, 4)(3, 2, 1, 4, 5)(3, 2, 4, 1, 5)(3, 4, 1, 5, 2)(3, 4, 1, 2, 5)(3, 4, 2, 1, 5)(2, 3, 1, 5, 4)(2, 3, 1, 4, 5)(2, 3, 4, 1, 5)(2, 4, 3, 1, 5)(4, 3, 1, 5, 2)(4, 3, 1, 2, 5)(4, 3, 2, 1, 5)(4, 2, 3, 1, 5)
喵喔喔
我给你另一种选择。import itertools a = [3,1,5]b = [2,4] c = [b +[a]][0] #0 to get only one level of nestedperm = [x for x in itertools.permutations(c, 3)] ans = []这是获得“ans”输出的公式:for i in range(len(perm)): groups = perm[i] #this is the subset like [2, 4, [3, 1, 5]] #flatten the list or returns integer clean = [y for x in groups for y in (x if isinstance(x,list) else [x])] ans.append(clean) print(clean)[[2, 4, 3, 1, 5], [2, 3, 1, 5, 4], [4, 2, 3, 1, 5], [4, 3, 1, 5, 2], [3, 1, 5, 2, 4], [3, 1, 5, 4, 2]]