Suppose I have two lists with word tokens of two sentences.
l1 = ['car', 'consumer', 'majority', 'prefer', 'red']
l2 = ['car', 'customer', 'maximum', 'preferred', 'red']
I want to compare the elements of these two lists, compare each word pair [(car, car) (car, customer)...] and compute similarity, if two words are similar score is one else compute the score to zero.
I am trying to create a 2d array where no. of rows represent words of l2 and no. of columns represent words of l1. Initially the values in the array are set to zero and then fill the array with the score by comparing each word pair.
I am trying to create a program but I am getting errors.
rows, cols = (len(l1),len(l2))
arr = [[0 for i in range(cols)] for j in range(rows)]
for j in arr:
for i in j:
if i==j:
result = 1
else:
result = 0
arr.append(result)
print(arr)
CodePudding user response:
You're looking for something like this?
arr = [1 if x == y else 0 for x,y in zip(l1,l2)]
Output:
[1,0,0,0,1]
CodePudding user response:
Looks like this is what you want :
l1 = ['car', 'consumer', 'majority', 'prefer', 'red']
l2 = ['car', 'customer', 'maximum', 'preferred', 'red']
l3 = list(zip(l1,l2))
for tup in l3[:]:
if tup[0] == tup[1]:
l3.remove(tup)
print(l3)