467,151 Members | 1,130 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,151 developers. It's quick & easy.

map float string '0.0' puzzle

Howdy,

I do not understand the following behavior for:
PythonWin 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on
win32.

float('0.0') 0.0 row = ('0.0', '1.0', None)
map(lambda setting: setting and float(setting) or None, row) [None, 1.0, None] map(lambda setting: setting and (setting,float(setting)) or (setting,None), row)
[('0.0', 0.0), ('1.0', 1.0), (None, None)]


Specifically, why is the return value of the first map operation:
[None, 1.0, None]

I was expecting:
[0.0, 1.0, None]
Thanks in advance for the help.
Jul 18 '05 #1
  • viewed: 1751
Share:
2 Replies
In article <bv**********@boulder.noaa.gov>, j vickroy
<ji*********@noaa.gov> wrote:
row = ('0.0', '1.0', None)
map(lambda setting: setting and float(setting) or None, row) [None, 1.0, None] map(lambda setting: setting and (setting,float(setting)) or (setting,None), row)
[('0.0', 0.0), ('1.0', 1.0), (None, None)]
Specifically, why is the return value of the first map operation:
[None, 1.0, None]


Break it down into something simpler so you can see what's going on.
Let's see what that lambda is evaluating to when setting is '0.0':
'0.0' and float('0.0') or None
print '0.0' and float('0.0') or None None

That expression evaluates as: ('0.0' and float('0.0')) or None
which means ('0.0' and float('0.0')) must be false.
'0.0' and float('0.0') 0.0

Now you see that the expression reduces to: 0.0 or None

The key is that 0.0 is considered false: bool(0.0) False print 0.0 or None

None

which means the expression reduced to the equivalent of: False or None
and so you get None as the result.

Hope that helps.

-Mark
Jul 18 '05 #2
At some point, "j vickroy" <ji*********@noaa.gov> wrote:
Howdy,

I do not understand the following behavior for:
PythonWin 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on
win32.

float('0.0') 0.0 row = ('0.0', '1.0', None)
map(lambda setting: setting and float(setting) or None, row) [None, 1.0, None] map(lambda setting: setting and (setting,float(setting)) or (setting,None), row)
[('0.0', 0.0), ('1.0', 1.0), (None, None)]
Specifically, why is the return value of the first map operation:
[None, 1.0, None]

I was expecting:
[0.0, 1.0, None]


Because float('0.0') is a false value -- 0.0 is taken as false. So
the lambda becomes
'0.0' and 0.0 or None
which evaluates to None.

It looks like you're trying to use the 'conditional expression' trick,
which fails in these situations. It'd be clearer to be more
expressive, and define a function:
def float_to_str(s): .... if s:
.... return float(s)
.... else:
.... return None
.... [ float_to_str(s) for s in row ]

[0.0, 1.0, None]

where I've used a list comprehension instead of map. In fact, I'd
probably define float_to_str like this:
def float_to_str(s):
try:
return float(s)
except:
return None

which now works for anything: if it's convertible to float, it returns
the float, otherwise None.

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by bofh1234@hotmail.com | last post: by
5 posts views Thread by ashish0799 | last post: by
14 posts views Thread by Jim Langston | last post: by
4 posts views Thread by honey777 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.