python - Permutations of the elements of two lists with order restrictions -
I would like to merge two input lists together and get all the permutations of their elements that the order of the elements of each input
For example, if I have two lists: where the sequence of elements of the elements are retained (comes before the BA, comes before cd). I have a problem that has been encountered in dealing with such a problem, but is being used as stars rather than lists:. For example, "BCD", "BCAD", "BCDA", "CABEd", "CBDA", "CDBA" will be returned as input in the form of wire "BA" and "CD". I have tried to optimize the code in my problem, but it is not working. Using the same example above, I hope that instead of 6 lists, I got [none, none, none, none, none] below is the code I am used to This then produces your required output: ['b', 'a'] and
['c', 'd'] , I would like to get the following lists: ,
def command_permutations (list1, list2): perms = [] if len (list1) + len (list2) == 1: return [list1 or list2] if list1: order_permutations for items (list 1 [1:] , List 2): perms.append ([list1 [0]]. Append (item)) if list2: item in order_permutations (list1, list2 [1:]): perms.append ([list2 [0] [] Append (return) permit
list.append () returns
none As the list changes in place, you should use concatenation instead:
& gt; & Gt; & Gt; Def series_changes (list1, list2): ... perms = [] ... if LAN (list1) + len (list2) == 1: ... return [list1 or list2] ... if list1:. .. order_permutations for item (list1 [1:], list2): ... perms.append ([list1 [0]] + item) ... if list2: ... in order to order items in order_permutations (list1, List2 [1:]): ... perms.append ([list2 [0]] + item) ... return perms ... & gt; & Gt; & Gt; For the combo, the order changes (['B', 'A'], ['C', 'D']): ... print combo ... ['B', 'A', 'C', 'D' ] ['B', 'C', 'A', 'D'] ['B', 'C', 'D', 'A'] ['C', 'B', 'A', 'D '' ['C', 'B', 'D', 'A'] ['C', 'D', 'B', 'A']
Comments
Post a Comment