type float problem

R

rolandschulz

Hi,

why does

from numpy import *
a=array([.2])
print map(type,map(float,a))
print "%f"%map(float,a)

give :TypeError: float argument required

This is confusing because line 3 returns [<type 'float'>].

This is on python 2.5.2 and numpy 1.2.1.

Roland
 
P

Pavol Juhas

This has nothing to do with numpy, the issue is the string "%"
operator
cannot convert list to a float (Line 3 in your example gives a list).
In '"%f" % x' the x has to be a number or tuple.

Does not work:
print "%f" % [1.0]

Works:
print "%f" % 1
print "%f" % (1, )
print "%f" % tuple([1])
print "%f" % a[0]

Hope this helps,

Pavol
 
R

rolandschulz

This has nothing to do with numpy, the issue is the string "%"
operator
cannot convert list to a float (Line 3 in your example gives a list).
In '"%f" % x' the x has to be a number or tuple.

Does not work:
    print "%f" % [1.0]

Yes that helps. Why is it that the %-operator requires a tuple and
does not work with a list?

Thus why doesn't this work: "%d %d"%[2,2]
but instead it has to be: "%d %d"%tuple([2,2])
or: "%d %d"%(2,2)

Thanks
Roland
 
R

Robert Kern

This has nothing to do with numpy, the issue is the string "%"
operator
cannot convert list to a float (Line 3 in your example gives a list).
In '"%f" % x' the x has to be a number or tuple.

Does not work:
print "%f" % [1.0]

Yes that helps. Why is it that the %-operator requires a tuple and
does not work with a list?

Thus why doesn't this work: "%d %d"%[2,2]
but instead it has to be: "%d %d"%tuple([2,2])
or: "%d %d"%(2,2)

Allowing only a single sequence type to represent a collection of arguments for
interpolation (as opposed to a single argument for a single % interpolation
marker) helps resolve some ambiguities:

'%s' % ['something']
'%s' % ('something',)

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
D

Dennis Lee Bieber

from numpy import *
a=array([.2])
print map(type,map(float,a))
print "%f"%map(float,a)

give :TypeError: float argument required

This is confusing because line 3 returns [<type 'float'>].

You'd have seen "why" if you were using the generic %s formatting
operation. About the only reason for not using %s is when one is
producing tabular reports and need to set specific widths and
precisions: %10.6f

Otherwise, %s will ask the object to format itself in its idea of a
printable representation.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top