473,654 Members | 3,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2006
In article <bv**********@b oulder.noaa.gov >, j vickroy
<ji*********@no aa.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*********@no aa.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)phy sics(dot)mcmast er(dot)ca
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
7606
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, Karthi
3
2711
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 structs stored on a server. The string (of a float number) stored is returned. The function works except that it does not return a float. It retreives the string fine, but I don't know why it is not converting the string to a float. What am I doing...
6
8558
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 circumstances(XML file source) and with exactly the same value it gets it perfectly correct all the time. These are the results I got, XML is always correct, CSV are only incorrect for some of the values (above about 0.01) but always gives the...
5
4460
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 is to solve jigsaw puzzles using the computer. The jigsaw puzzle here is a square of dimension d (a puzzle with d^2 pieces) and the jigsaw pieces (all same dimensions) are of dimensions H x W (Which means the pieces have ‘H’ rows of ‘W’...
3
3201
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 to bottom (S), or (iv) bottom to up (N). The program will print the starting place and the direction of each word. Limitations The number of words to be searched can be at most 100, the size of the puzzle N can be minimum 5 maximum 20....
12
11277
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 to do : #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <fstream> using namespace std; using std::ifstream; using std::ofstream;
14
5534
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
4419
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 looks like this header file
4
19932
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 get the tiles in order, 1 through 15, from left to right, top to bottom, by just sliding tiles into the empty square. In this configuration, the goal would be to get the 14 and 15 to switch places, without affecting any of the other squares. Your...
0
8285
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8814
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8706
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8475
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8591
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7304
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5621
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.