python - Comparing sub items from a list of lists -
i,ve 2 lists:
l = [['red','a1',1],['red','a2',1],['blue','a3',1],['yellow','a4',1]]
and
k = [['red','a2',1],['blue','a3',1],['yellow','a4',1]]
so want return this:
result = [0, 1, 1, 1]
sorry i´ve practice list comprehension little more!!
my function:
def vectors(doc1,doc2,consulta): res=[] r = doc1 + doc2 + consulta e in r: in doc1: if i[0] == e[0]: i[2] = i[2] + 1 else: i[2] = 0 return res.append(i[2])
the order doesn´t matter, important thing comparison.
best regards!
inefficient easy:
result = [x in k x in l]
efficient (for large k
) more complicated:
kset = set(tuple(x) x in k) result = [tuple(x) in kset x in l]
Comments
Post a Comment