Minimum and Maximum of a list containing floating point numbers

C

ceycey

I have a list like ['1.1881', '1.1881', '1.1881', '1.1881', '1.1881',
'1.1881', '1.1881', '1.1881', '1.1881', '1.1881', '1.7689', '1.7689',
'3.4225', '7.7284', '10.24', '9.0601', '9.0601', '9.0601', '9.0601',
'9.0601']. What I want to do is to find minimum and maximum number in
this list.

I used min function,

s = ['1.1881', '1.1881', '1.1881', '1.1881', '1.1881', '1.1881',
'1.1881', '1.1881', '1.1881', '1.1881', '1.7689',
'1.7689', '3.4225', '7.7284', '10.24', '9.0601', '9.0601', '9.0601',
'9.0601', '9.0601']

print min(s)
print max(s)

these gives me

1.181
9.0601

maximum value is wrong. It must be 10.24.

I know why max function gives wrong number. Because max function
processed elements of list as strings. How can I convert the elements
of list to float so max function finds the correct answer.

I hope I can explain my problem.

Cuneyt.
 
M

MRAB

On 7 September 2010 10:37, ceycey <[email protected]

I have a list like ['1.1881', '1.1881', '1.1881', '1.1881', '1.1881',
'1.1881', '1.1881', '1.1881', '1.1881', '1.1881', '1.7689', '1.7689',
'3.4225', '7.7284', '10.24', '9.0601', '9.0601', '9.0601', '9.0601',
'9.0601'].

How can I convert the elements
of list to float so max function finds the correct answer.

input = ['3.4225', '7.7284', '10.24']
[float(x) for x in input]
[3.4225, 7.7284, 10.24]
If you wanted to find the maximum value without converting the list to
numbers you could do this:
>>> input = ['3.4225', '7.7284', '10.24']
>>> max(input, key=float)
'10.24'

Incidentally, there's a builtin function called 'input' so using it as
a variable name is a discouraged! :)
 
T

Tim Chase

I have a list like ['1.1881', '1.1881', '1.1881', '1.1881', '1.1881',
'1.1881', '1.1881', '1.1881', '1.1881', '1.1881', '1.7689', '1.7689',
'3.4225', '7.7284', '10.24', '9.0601', '9.0601', '9.0601', '9.0601',
'9.0601']. What I want to do is to find minimum and maximum number in
this list.

I used min function,

print min(s)
print max(s)

these gives me

1.181
9.0601

maximum value is wrong. It must be 10.24.

I know why max function gives wrong number. Because max function
processed elements of list as strings. How can I convert the elements
of list to float so max function finds the correct answer.

You can use

min(float(v) for v in s)
max(float(v) for v in s)

to return the floating-point number, or in Python2.5+ you can use

min(s, key=float)
max(s, key=float)

to get the string source. If you need the source string in
pre-2.5, you'd have to do something like

min((float(v), v) for v in s)[1] # 2.4
min([(float(v), v) for v in s])[1] # 2.3 or earlier

and guard against empty input lists.

-tkc
 
A

Albert Hopkins

I have a list like ['1.1881', '1.1881', '1.1881', '1.1881', '1.1881',
'1.1881', '1.1881', '1.1881', '1.1881', '1.1881', '1.7689', '1.7689',
'3.4225', '7.7284', '10.24', '9.0601', '9.0601', '9.0601', '9.0601',
'9.0601']. What I want to do is to find minimum and maximum number in
this list.

I used min function,

s = ['1.1881', '1.1881', '1.1881', '1.1881', '1.1881', '1.1881',
'1.1881', '1.1881', '1.1881', '1.1881', '1.7689',
'1.7689', '3.4225', '7.7284', '10.24', '9.0601', '9.0601', '9.0601',
'9.0601', '9.0601']

print min(s)
print max(s)

these gives me

1.181
9.0601

maximum value is wrong. It must be 10.24.

You are not comparing a list of floats but a list of strings.
I know why max function gives wrong number. Because max function
processed elements of list as strings. How can I convert the elements
of list to float so max function finds the correct answer.

min/max in these cases are returning strings as well. So the fact
remains that the max function is not giving you a number at all, but a
string, and as such is correct. String comparison is not identical to
numerical comparison.

But to answer your question:
s = ['1.1881', '1.1881', '1.1881', '1.1881', '1.1881', '1.1881',
.... '1.1881', '1.1881', '1.1881', '1.1881', '1.7689',
.... '1.7689', '3.4225', '7.7284', '10.24', '9.0601', '9.0601', '9.0601',
.... '9.0601', '9.0601']
[<type 'str'>, <type 'str'>, <type 'str'>, <type 'str'>, <type 'str'>,
<type 'str'>, <type 'str'>, <type 'str'>, <type 'str'>, <type 'str'>,
<type 'str'>, <type 'str'>, <type 'str'>, <type 'str'>, <type 'str'>,
[QUOTE= said:
type(max(s))
t = [float(x) for x in s]
[type(x) for x in t]
[/QUOTE]
[<type 'float'>, <type 'float'>, <type 'float'>, <type 'float'>, <type
'float'>, <type 'float'>, <type 'float'>, <type 'float'>, <type
'float'>, <type 'float'>, <type 'float'>, <type 'float'>, <type
10.24
[/QUOTE]
<type 'float'>
 
N

nn

If you're going to use the list of float objects, you can convert them
all with a list comprehension. [...]
    >>> numbers_as_float = [float(x) for x in numbers_as_str]

That's awfully verbose. A map is simpler:

numbers_as_float = map(float, numbers_as_str)

In Python 3.x it has one disadvantage:
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
min(numbers_as_float)
ValueError: min() arg is an empty sequence
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top