sang
sang

Reputation: 405

identical word alignment using for loop

I'm trying to align words from two list

sentence1 = ['boy','motorcycle','people','play']
sentence2 = ['run','boy','people','boy','play','play']

and this is my codes :

def identicalWordsIndex(self, sentence1, sentence2):
    identical_index = []
    for i in xrange(len(sentence1)):
        for j in xrange(len(sentence2)):
            if sentence1[i] == sentence2[j]:
                idenNew1 = [i,j]
                identical_index.append(idenNew1)
            if sentence2[j] == sentence1[i]:
                idenNew2 = [j,i]
                identical_index.append(idenNew2)
    return identical_index

what i'm trying to do is get the index number of align words from sentence1 and sentence2.

1st is the aligned words index from sentence1 towards sentence2. 2nd is the aligned words index from sentence2 towards sentence1.

but the result from the codes above is like this :

1st : [[0, 1], [1, 0], [0, 3], [3, 0], [2, 2], [2, 2], [3, 4], [4, 3], [3, 5], [5, 3]]
2nd : [[0, 1], [1, 0], [0, 3], [3, 0], [2, 2], [2, 2], [3, 4], [4, 3], [3, 5], [5, 3]]

what I expect from the result is like this :

1st : [[0,1],[2,2],[3,4]]
2nd : [[1,0],[2,2],[3,0],[4,3],[5,3]]

anyone can solve? thanks

Upvotes: 0

Views: 495

Answers (3)

Brian Thorpe
Brian Thorpe

Reputation: 327

You just need to add breaks. Try this:

sentence1 = ['boy','motorcycle','people','play']
sentence2 = ['run','boy','people','boy','play','play']
identical_index = []

def identicalWordsIndex( sentence1, sentence2):
    identical_index = []
    for i in xrange(len(sentence1)):
        for j in xrange(len(sentence2)):
            if sentence1[i] == sentence2[j]:
                idenNew1 = [i,j]
                identical_index.append(idenNew1)
                break
    return identical_index

print (identicalWordsIndex(sentence1, sentence2))
print (identicalWordsIndex(sentence2, sentence1))

Prints:

[[0, 1], [2, 2], [3, 4]]

[[1, 0], [2, 2], [3, 0], [4, 3], [5, 3]]

Upvotes: 1

Tiger-tail
Tiger-tail

Reputation: 11

See if this suits you. At the last two lines, you can just swap the arguments to get what you want.

sentence1 = ['boy','motorcycle','people','play']
sentence2 = ['run','boy','people','boy','play','play']

def identicalWordsIndex(sentence1, sentence2):
    identical_index = []
    for i in range(len(sentence1)):
        for j in range(len(sentence2)):
            if sentence1[i] == sentence2[j]:
            identical_index.append([i, j])
                break
    return identical_index

print(identicalWordsIndex(sentence1, sentence2))
print(identicalWordsIndex(sentence2, sentence1))

output:

>>>[[0, 1], [2, 2], [3, 4]]
>>>[[1, 0], [2, 2], [3, 0], [4, 3], [5, 3]]

Upvotes: 1

Chiheb Nexus
Chiheb Nexus

Reputation: 9257

You can try this solution using for loops:

a = ['boy','motorcycle','people','play']
b = ['run','boy','people','boy','play','play']

def align_ab(a, b):
    indexed = []
    for k,v in enumerate(a):
        try:
            i = b.index(v)
            indexed.append([k,i])
        except ValueError:
            pass

    return indexed
# Align a words from b
print(align_ab(a,b))
# Align b words from a
print(align_ab(b,a))

Output:

>>> [[0, 1], [2, 2], [3, 4]]
>>> [[1, 0], [2, 2], [3, 0], [4, 3], [5, 3]]

Upvotes: 1

Related Questions