Lambda with itertools now works

I want to create a cartesian product and select tuples from second and third position that are equal or different by only 1

import itertools

a = [(1,4), (2,2), (3,9),(7,4),(5,4)];

a1 = ( list(itertools.product(*a)))

print (a1)
i=a1[1]
k=a1[2]


new_list = list(filter(lambda i,k: (i-k <= 1) , a1))
print (my_list)

      

But the result

[(1, 2, 3, 7, 5), (1, 2, 3, 7, 4), (1, 2, 3, 4, 5), (1, 2, 3, 4, 4), (1, 2, 9, 7, 5), (1, 2, 9, 7, 4), (1, 2, 9, 4, 5), (1, 2, 9, 4, 4), (1, 2, 3, 7, 5), (1, 2, 3, 7, 4), (1, 2, 3, 4, 5), (1, 2, 3, 4, 4), (1, 2, 9, 7, 5), (1, 2, 9, 7, 4), (1, 2, 9, 4, 5), (1, 2, 9, 4, 4), (4, 2, 3, 7, 5), (4, 2, 3, 7, 4), (4, 2, 3, 4, 5), (4, 2, 3, 4, 4), (4, 2, 9, 7, 5), (4, 2, 9, 7, 4), (4, 2, 9, 4, 5), (4, 2, 9, 4, 4), (4, 2, 3, 7, 5), (4, 2, 3, 7, 4), (4, 2, 3, 4, 5), (4, 2, 3, 4, 4), (4, 2, 9, 7, 5), (4, 2, 9, 7, 4), (4, 2, 9, 4, 5), (4, 2, 9, 4, 4)]


    new_list = list(filter(lambda i,k: (i-k <= 1) , a1))
TypeError: <lambda>() missing 1 required positional argument: 'k'

      

What's wrong with my lambda?

+3


source to share


1 answer


import itertools
a =[(1,4),(2,2),(3,9),(7,4),(5,4)]
b = itertools.product(*a)
c = filter(lambda x: abs(x[1]-x[2])<=1, b)
print(c)

      

gives (edited for visibility)



[(1, 2, 3, 7, 5), 
 (1, 2, 3, 7, 4), 
 (1, 2, 3, 4, 5), 
 (1, 2, 3, 4, 4), 
 (1, 2, 3, 7, 5), 
 (1, 2, 3, 7, 4),
 (1, 2, 3, 4, 5),
 (1, 2, 3, 4, 4),
 (4, 2, 3, 7, 5), 
 (4, 2, 3, 7, 4), 
 (4, 2, 3, 4, 5), 
 (4, 2, 3, 4, 4), 
 (4, 2, 3, 7, 5), 
 (4, 2, 3, 7, 4), 
 (4, 2, 3, 4, 5), 
 (4, 2, 3, 4, 4)]

      

+3


source







All Articles