f.rodrigues
f.rodrigues

Reputation: 3587

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?

Upvotes: 1

Views: 318

Answers (3)

Stefan Pochmann
Stefan Pochmann

Reputation: 28636

True, map doesn't unpack, but starmap does:

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

Upvotes: 3

Phylogenesis
Phylogenesis

Reputation: 7890

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)

Upvotes: 2

Cory Kramer
Cory Kramer

Reputation: 117981

The equivalent generator expression would be

>>> ip = "128.32.10.1"
>>> sum(int(base) * 256 ** (3 - exp) for exp, base in enumerate(ip.split('.')))
2149583361

Upvotes: 2

Related Questions