Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Minimum and Maximum of a list containing floating point numbers

Reply
Thread Tools

Minimum and Maximum of a list containing floating point numbers

 
 
ceycey
Guest
Posts: n/a
 
      09-07-2010
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.
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      09-07-2010
On 07/09/2010 01:44, Xavier Ho wrote:
> On 7 September 2010 10:37, ceycey <(E-Mail Removed)
> <(E-Mail Removed)>> wrote:
>
> 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!
 
Reply With Quote
 
 
 
 
Tim Chase
Guest
Posts: n/a
 
      09-07-2010
On 09/06/10 19:37, ceycey wrote:
> 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




 
Reply With Quote
 
Albert Hopkins
Guest
Posts: n/a
 
      09-07-2010
On Mon, 2010-09-06 at 17:37 -0700, ceycey wrote:
> 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(x) for x in s]

[<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'>,
<type 'str'>, <type 'str'>, <type 'str'>, <type 'str'>, <type 'str'>]


>>> type(max(s))

<type 'str'>

>>> t = [float(x) for x in s]
>>> [type(x) for x in t]

[<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
'float'>, <type 'float'>, <type 'float'>, <type 'float'>, <type
'float'>, <type 'float'>, <type 'float'>, <type 'float'>]
>>> min(t)

1.1880999999999999
>>> max(t)

10.24

>>> type(max(s))

<type 'str'>
>>> type(max(t))

<type 'float'>


 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-07-2010
On Tue, 07 Sep 2010 11:00:45 +1000, Ben Finney wrote:

> 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)


--
Steven

 
Reply With Quote
 
nn
Guest
Posts: n/a
 
      09-07-2010
On Sep 6, 10:31*pm, Steven D'Aprano <steve-REMOVE-
(E-Mail Removed)> wrote:
> On Tue, 07 Sep 2010 11:00:45 +1000, Ben Finney wrote:
> > 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)
>
> --
> Steven


In Python 3.x it has one disadvantage:

>>> numbers_as_float = map(float, numbers_as_str)
>>> max(numbers_as_float)

10.24
>>> min(numbers_as_float)

Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
min(numbers_as_float)
ValueError: min() arg is an empty sequence
>>>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What are the minimum and maximum float numbers and integers? Peng Yu Perl Misc 16 01-31-2010 10:23 AM
How to find and deal with maximum and minimum numbers? zj262144@163.com C Programming 8 10-19-2008 01:04 PM
Minimum sizes of integral and floating point types Ioannis Vranos C++ 13 03-10-2008 06:29 PM
Minimum sizes of integral and floating point types Ioannis Vranos C Programming 9 03-09-2008 10:43 AM
Fixed-point format for floating-point numbers Motaz Saad Java 7 11-05-2005 05:33 PM



Advertisments