I got this list:
verticeToCompare=['Q']
a=[[0, 1, 2, 1, 9, 'a'], [1, 0, 0, 6, 0, 'c'], [2, 0, 0, 15, 2, 'g'], [1, 6, 15, 0, 7, 'w'], [9, 0, 2, 7, 0, 'Q']]
b=[[0, 19, 1, 0, 12, 0, 'a'], [19, 0, 2, 0, 0, 0, 'c'], [1, 2, 0, 0, 2, 0, 'w'], [0, 0, 0, 0, 3, 5, 'Q'], [12, 0, 2, 3, 0, 2, 'front'], [0, 0, 0, 5, 2, 0, 'Z']]
for item in verticeToCompare:
for itemTwo in firstMatrixOriginal:
if item==itemTwo[-1]:
listToCompare.append(itemTwo)
for item in verticeToCompare:
for itemTwo in secondMatrixOriginal:
if item==itemTwo[-1]:
listToCompareTwo.append(itemTwo)
You can use a list comprehension with a nested loop, it'll return elements from a
, followed by those from b
.
>>> [y for x in verticeToCompare for y in a + b if y[-1] == x]
[[9, 0, 2, 7, 0, 'Q'], [0, 0, 0, 0, 3, 5, 'Q']]
However, if your ending strings are unique, you should consider using a dictionary of ending strings : values
to store your data instead, so you'd then be able to access values associated with ending strings in constant time.