472,331 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,331 software developers and data experts.

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
2 1933
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my...
3
by: bofh1234 | last post by:
I am trying to write a function that returns a float. This is what the function should do: It takes the socket number and a variable name. The...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some...
5
by: ashish0799 | last post by:
HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM Jigsaw puzzles. You would have solved many in your childhood and many people still like it in...
3
by: oncue01 | last post by:
Word Puzzle Task You are going to search M words in an N N puzzle. The words may have been placed in one of the four directions as from (i) left...
12
by: joestevens232 | last post by:
Okay, Im having some problems with my code. Im trying to use the <cstdlib> library and im trying to convert string data at each whitespace slot. I...
14
by: Jim Langston | last post by:
The output of the following program is: 1.#INF 1 But: 1.#INF 1.#INF was expected and desired. How can I read a value of infinity from a...
8
by: Ruben | last post by:
error: passing `const Weight' as `this' argument of `float Weight::wgt()' discards qualifiers seems to be some sort of standard error format...
4
by: honey777 | last post by:
Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.