Tuesday, 20 August 2013

filling with zeros the list obtained from a comparison

filling with zeros the list obtained from a comparison

I have this function that receives 3 lists:
DOCUMENTO 1
[['ser', 'VSIS3S0', 1], ['francisco_villa', 'NP00000', 2], ['norte',
'NCMS000', 1], ['revolucion_mexicana', 'NP00000', 1], ['nombrar',
'VMP00SM', 1], ['centauro', 'NCMS000', 1]] DOCUMENTO 2 [['pintor',
'NCMS000', 1], ['ser', 'VSIS3S0', 1], ['muralista', 'AQ0CS0', 1],
['diego_rivera', 'NP00000', 1], ['frida_kahlo', 'NP00000', 1], ['caso',
'NCMS000', 1]] CONSLUTA [['ser', 'VSIP3S0', 1], ['francisco_villa',
'NP00000', 1], ['quien', 'NP00000', 1]]
function:
def vectores(doc1,doc2,consulta):
res=[]
l1=[]
cont = 0
r = doc1 + doc2 + consulta
for i in r:
l1.append(i[0])
for e in doc1:
if e[0] in l1:
res.append(e[2])
else:
res.append(e[0]==0 * len(l1))
return res
[1, 2, 1, 1, 1, 1] -> res
I need to compare if the key[0] of doc1 exists in key[0] of l1 then append
key[2] to the res, and if they don't match append a zero, forming a vector
with lenght of l1
Like this output:[1, 2, 1, 1, 1, 1, 0, 0, 0, ...]
Thank you in advance! ;)

No comments:

Post a Comment