473,414 Members | 1,686 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 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 1989
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 processor memory I can't use it in my code. regards,...
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 function looks up the variable in an array of...
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 circumstances(CSV file source) but in very similar...
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 their old ages also. Now what you have got to do...
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 to right (E), (ii) right to left (W), (iii) up...
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 think if you see my code you'll get what im trying...
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 stream?
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 that I'm not understanding. I have code that...
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 an example of the puzzle: The goal is to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.