Mary K
Mary K

Reputation: 45

Possible alignments of two lists using Python

I have two strings in two different lists A = [dog bit dog null] and B = [hund bet hund]. I would like to find all possible allignments from list B to list A such as :

  C =  [(hund = dog, bet = bit, hund = dog),
        (hund = dog, bet = bit, hund = bit),
        (hund = dog, bet = bit, hund = null),
        (hund = dog, bet = dog, hund = dog),
        (hund = dog, bet = dog, hund = bit),
        etc.. ]

I think there are 64 different allignments between these two strings. I am working on the IBM model1 for word translastion.

Upvotes: 0

Views: 284

Answers (2)

DSM
DSM

Reputation: 352989

If you want the 64 possibilities, you could use itertools.product:

>>> from itertools import product
>>> A = "dog bit dog null".split()
>>> B = "hund bet hund".split()
>>> product(A, repeat=3)
<itertools.product object at 0x1148fd500>
>>> len(list(product(A, repeat=3)))
64
>>> list(product(A, repeat=3))[:5]
[('dog', 'dog', 'dog'), ('dog', 'dog', 'bit'), ('dog', 'dog', 'dog'), ('dog', 'dog', 'null'), ('dog', 'bit', 'dog')]

but note that this is going to generate a fair number of duplicates, given that you have dog twice in A:

>>> len(set(product(A, repeat=3)))
27

You could even get the associated triplets of pairs if you wanted:

>>> trips = [zip(B, p) for p in product(A, repeat=len(B))]
>>> trips[:5]
[[('hund', 'dog'), ('bet', 'dog'), ('hund', 'dog')], [('hund', 'dog'), ('bet', 'dog'), ('hund', 'bit')], [('hund', 'dog'), ('bet', 'dog'), ('hund', 'dog')], [('hund', 'dog'), ('bet', 'dog'), ('hund', 'null')], [('hund', 'dog'), ('bet', 'bit'), ('hund', 'dog')]]

Upvotes: 1

LtWorf
LtWorf

Reputation: 7600

[(i,j) for i in a for j in b]

You can't have that structure in a list, you'd need a dictionary, I am using a tuple here to associate the values.

Upvotes: 0

Related Questions