I
I
Ilya2016-05-02 14:22:02
Python
Ilya, 2016-05-02 14:22:02

How to use lambda expressions?

Good afternoon, help to correct a part of the code. I found a library but it was written for the second python, it swears at a line with a lambda expression.

def avhash(im):
    if not isinstance(im, Image.Image):
        im = Image.open(im)
    im = im.resize((8, 8), Image.ANTIALIAS).convert('L')
    avg = reduce(lambda x, y: x + y, im.getdata()) / 64.
    return reduce(lambda x, (y, z): x | (z << y),
                  enumerate(map(lambda i: 0 if i < avg else 1, im.getdata())),
                  0)

Mistake
Traceback (most recent call last):
  File "tt.py", line 27, in <module>
    hash1 = avhash(img1)
  File "tt.py", line 11, in avhash
    return functools.reduce(lambda x, y, z: x | z << y, enumerate(map(lambda i: 0 if i < avg else 1, im.getdata())),0)
TypeError: <lambda>() missing 1 required positional argument: 'z'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2016-05-02
@nuBacuk

Try lambda x, (y, z): x | (z << y)replacing with lambda x, yz: x | (yz[1] << yz[0]). Cartage unpacking in options has been removed from python 3 as per PEP 3113 .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question