Search code examples
pythonpython-2.7dictionaryiterable-unpacking

Map not unpacking tuples


I have this simple formula that converts an IP to a 32 bits integer:

(first octet * 256**3) + (second octet * 256**2) + (third octet * 256**1) + (fourth octet)

I made a program that does that:

def ip_to_int32(ip):
    # split values
    ip = ip.split(".")

    # formula to convert to 32, x is the octet, y is the power
    to_32 = lambda x, y: int(x) * 256** (3 - y)

    # Sum it all to have the int32 of the ip
    # reversed is to give the correct power to octet
    return sum(
        to_32(octet, pwr) for pwr, octet in enumerate(ip)
    )

 ip_to_int32("128.32.10.1") # --> 2149583361

And it works as intended.

Then I tried to make a one-liner, just for the sake of doing it.

sum(map(lambda x, y: int(x) * 256 ** (3 - y), enumerate(ip.split("."))))

But this raises

TypeError: <lambda>() takes exactly 2 arguments (1 given)

So the tuple (y, x) is not being unpacked. I can fix this with

sum(map(lambda x: int(x[1]) * 256 ** (3 - x[0]), enumerate(ip.split("."))))

But this seems uglier (one liners are always ugly)

I even tried using a list comprehensions, but map still doesn't unpack the values.

Is this a feature or am I doing something wrong? Is there a specific way to do this?


Solution

  • The following is probably a little tidier (using reduce() as I suggested in my comment)

    reduce(lambda a, b: a * 256 + int(b), ip.split("."), 0)