I have lists A and B, which can have duplicates, for example:
A = ['x', 'x', 7]
B = [7, 'x', 'x']
Now I want all index permutations that permute list B into list A:
[1, 2, 0] # because [B[1], B[2], B[0]] == A
[2, 1, 0] # because [B[2], B[1], B[0]] == A
Is there are way to achieve this without iterating over all possible permutations? I already use
import itertools
for p in itertools.permutations(range(len(B))):
if A == permute(B,p):
to iterate over all possible permutations and check for the ones I want, but I want to have the right ones faster.
You should decompose your problem in two :
sigma_0
that maps B
onto A
S_B
of all the permutations that map B onto itself Then the set you are looking after is just {sigma_0 \\circ \\sigma, sigma \\in S_B}
.
Now the question becomes : how to we determine S_B
? To do this, you can just observe that if you write the set {0,1,2,..,n}
(with n=2
in your case) as A_1 \\cup .. A_k
, where each A_i
corresponds to the indices in B
that correspond to the i-th element (in your case, you would have A_1 = {1,2}
and A_2 = {0}
), then each element of S_B
can be written in a unique manner as a product tau_1 \\circ .. tau_k
where each tau_i
is a permutation that acts on A_i
.
So, in your case S_B = {id, (1,2)}
and you can take sigma_0 = (0,2)
. It follows that the set your are after is {(0,2), (2,0,1)}
.
Here's one way to do it. My perms
function generates all valid permutations. First I collect the indexes for each element in B, then I recursively build and yield the permutations by always picking one of the still available indexes for each item in A until a permutation is ready to be yielded.
from collections import defaultdict
def perms(A, B):
indexes = defaultdict(set)
for i, e in enumerate(B):
indexes[e].add(i)
def find(perm):
k = len(perm)
if k == len(A):
yield perm
return
I = indexes[A[k]]
for i in list(I):
I.remove(i)
yield from find(perm + (i,))
I.add(i)
yield from find(())
Usage:
A = ['x', 'x', 7]
B = [7, 'x', 'x']
for perm in perms(A, B):
print(perm)
Output:
(1, 2, 0)
(2, 1, 0)
You could do this in three steps. First, create a dictionary from list B that has items as keys and indexes as values. In your case, the dictionary for your list B:
B = [7, 'x', 'x']
7 - [0]
'x' - [1, 2]
Then, go through your list A and build an index that maps List A indexes to their corresponding List B indexes. That is:
A = ['x', 'x', 7]
0 - [1, 2]
1 - [1, 2]
2 - [0]
From that, you can generate all of the valid mappings. It should be easy enough to write code that, given the index above, will generate [1, 2, 0]
and [2, 1, 0]
.
Here's something in Python. I used strings for hashing since I'm not familiar with how to pass sets and arrays as recursive arguments in Python, although they might be more efficient.
A = ['x', 'x', 7, 'y', 8, 8]
B = [7, 'x', 8, 'x', 8, 'y']
H = {}
# record the indexes of elements in B
for i in xrange(0,len(B)):
if B[i] in H:
H[ B[i] ].append(str(i))
else:
H[ B[i] ] = [str(i)]
# build permutations in the order that the elements are encountered in A
def perms(perm,i,visited,l):
if i==l:
print perm
return
for j in H[ A[i] ]:
if j not in visited:
perms(perm + j,i + 1,visited + j,l)
perms("",0,"",len(A))
Output:
130524
130542
310524
310542
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.