From: wheres pythonmonks on
A new python convert is now looking for a replacement for another perl idiom.

In particular, since Perl is weakly typed, I used to be able to use
unpack to unpack sequences from a string, that I could then use
immediately as integers.

In python, I find that when I use struct.unpack I tend to get strings.
(Maybe I am using it wrong?)

def f(x,y,z): print x+y+z;

f( *struct.unpack('2s2s2s','123456'))
123456

(the plus concatenates the strings returned by unpack)

But what I want is:

f( *map(lambda x: int(x), struct.unpack('2s2s2s','123456')))
102

But this seems too complicated.

I see two resolutions:

1. There is a way using unpack to get out string-formatted ints?

2. There is something like map(lambda x: int(x).... without all the
lambda function call overhead. (e.g., cast tuple)?
[And yes: I know I can write my own "cast_tuple" function -- that's
not my point. My point is that I want a super-native python inline
solution like (hopefully shorter than) my "map" version above. I
don't like defining trivial functions.]

W
From: Nick Raptis on
On 07/28/2010 04:15 PM, wheres pythonmonks wrote:
>
> f( *map(lambda x: int(x), struct.unpack('2s2s2s','123456')))
> 102
>
> But this seems too complicated.
>
>
>
Well, you don't need the lambda at all
int === lambda x: int(x)

So just write

It's like writing:
def myint(x):
return int(x)


Nick,

Warm thanks to Steven D' Aprano who taught me that just yesterday in the
Tutor list ;)
From: Nick Raptis on
Ep, that missing line should be:

On 07/28/2010 04:27 PM, Nick Raptis wrote:
> On 07/28/2010 04:15 PM, wheres pythonmonks wrote:
>>
>> f( *map(lambda x: int(x), struct.unpack('2s2s2s','123456')))
>> 102
>>
>> But this seems too complicated.
>>
>>
> Well, you don't need the lambda at all
> int === lambda x: int(x)
>
> So just write
>
f( *map(int, struct.unpack('2s2s2s', '123456')))

Pretty compact now, isn't it?

> It's like writing:
> def myint(x):
> return int(x)
>
>
> Nick,
>
> Warm thanks to Steven D' Aprano who taught me that just yesterday in
> the Tutor list ;)
From: wheres pythonmonks on
Thanks ... I thought int was a type-cast (like in C++) so I assumed I
couldn't reference it.



On Wed, Jul 28, 2010 at 9:31 AM, Nick Raptis <airscorp(a)otenet.gr> wrote:
> Ep, that missing line should be:
>
> On 07/28/2010 04:27 PM, Nick Raptis wrote:
>>
>> On 07/28/2010 04:15 PM, wheres pythonmonks wrote:
>>>
>>> f( *map(lambda x: int(x), struct.unpack('2s2s2s','123456')))
>>> 102
>>>
>>> But this seems too complicated.
>>>
>>>
>> Well, you don't need the lambda at all
>> int   ===    lambda x: int(x)
>>
>> So just write
>>
> f( *map(int, struct.unpack('2s2s2s', '123456')))
>
> Pretty compact now, isn't it?
>
>> It's like writing:
>> def myint(x):
>>    return int(x)
>>
>>
>> Nick,
>>
>> Warm thanks to Steven D' Aprano who taught me that just yesterday in the
>> Tutor list ;)
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
From: Hexamorph on
wheres pythonmonks wrote:
> A new python convert is now looking for a replacement for another perl idiom.
>
> In particular, since Perl is weakly typed, I used to be able to use
> unpack to unpack sequences from a string, that I could then use
> immediately as integers.
>
> In python, I find that when I use struct.unpack I tend to get strings.
> (Maybe I am using it wrong?)
>
> def f(x,y,z): print x+y+z;
>
> f( *struct.unpack('2s2s2s','123456'))
> 123456
>
> (the plus concatenates the strings returned by unpack)
>
> But what I want is:
>
> f( *map(lambda x: int(x), struct.unpack('2s2s2s','123456')))
> 102
>
> But this seems too complicated.
>
> I see two resolutions:
>
> 1. There is a way using unpack to get out string-formatted ints?
>
> 2. There is something like map(lambda x: int(x).... without all the
> lambda function call overhead. (e.g., cast tuple)?
> [And yes: I know I can write my own "cast_tuple" function -- that's
> not my point. My point is that I want a super-native python inline
> solution like (hopefully shorter than) my "map" version above. I
> don't like defining trivial functions.]
>
> W

In [31]: import re

In [32]: sum(int(x) for x in re.findall("..", s))
Out[32]: 102

To get a tuple instead of the sum, just do:

In [33]: tuple(int(x) for x in re.findall("..", s))
Out[33]: (12, 34, 56)