Lack of Python know-how. I'm aware of how easy this is with a for
loop but I'm curious about the capabilities of python.
I have two parallel arrays that have comparable strings. I would like to enumerate over the first string so I can save the indexes, then I can use it to compare. However, I also need to perform a lambda function on the resulting array using both arguments.
filter(lambda x,y: y == arr2[x], list(enumerate(arr1))
The lambda function fails because enumerate returns a list of tuples, and the lambda function interprets them as single arguments.
is there an inline alternative to enumerate
that would give me the result I want? (array of arrays)
current output: [(0,value),(1,value),(2,value)...]
desired output: [[0,value],[1,value],[2,value]...]
It is possible with the zip
function. zip built-in function doc.
arr1 = [1, 2, 4]
arr2 = [1, 3, 4]
filter(lambda p: p[0] == p[1], [pair for pair in zip(arr1, arr2)])
zip
returns iterable of pairs that contains values of elements with the same indexes.
Update: yes, the solution may be simplified
filter(lambda p: p[0] == p[1], zip(arr1, arr2))