Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Minimum and Maximum of a list containing floating point numbers

1 view
Skip to first unread message

ceycey

unread,
Sep 6, 2010, 8:37:33 PM9/6/10
to
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.

MRAB

unread,
Sep 6, 2010, 8:56:58 PM9/6/10
to Python List
On 07/09/2010 01:44, Xavier Ho wrote:
> On 7 September 2010 10:37, ceycey <cuneyt...@gmail.com

> <mailto:cuneyt...@gmail.com>> 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! :-)

Ben Finney

unread,
Sep 6, 2010, 9:00:45 PM9/6/10
to
ceycey <cuneyt...@gmail.com> writes:

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

As you correctly describe later, there aren't any numbers in that list;
only strings.

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

If you're going to use the list of float objects, you can convert them
all with a list comprehension.

>>> numbers_as_str = ['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']
>>> numbers_as_float = [float(x) for x in numbers_as_str]
>>> print min(numbers_as_float), max(numbers_as_float)
1.1881 10.24

--
\ “As scarce as truth is, the supply has always been in excess of |
`\ the demand.” —Josh Billings |
_o__) |
Ben Finney

Tim Chase

unread,
Sep 6, 2010, 9:00:39 PM9/6/10
to ceycey, pytho...@python.org
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


Albert Hopkins

unread,
Sep 6, 2010, 9:01:29 PM9/6/10
to pytho...@python.org
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'>


Steven D'Aprano

unread,
Sep 6, 2010, 10:31:17 PM9/6/10
to
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

Ben Finney

unread,
Sep 6, 2010, 10:40:57 PM9/6/10
to

I'll pay “verbose”, but not “awfully”. There is little difference in
verbosity between your example and mine. Further, I don't see my example
as excessive, which I assume your “awfully” entails.

--
\ “I call him Governor Bush because that's the only political |
`\ office he's ever held legally.” —George Carlin, 2008 |
_o__) |
Ben Finney

Steven D'Aprano

unread,
Sep 6, 2010, 11:50:41 PM9/6/10
to
On Tue, 07 Sep 2010 12:40:57 +1000, Ben Finney wrote:

> Steven D'Aprano <steve-RE...@cybersource.com.au> writes:
>
>> 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)
>
> I'll pay “verbose”, but not “awfully”. There is little difference in
> verbosity between your example and mine. Further, I don't see my example
> as excessive, which I assume your “awfully” entails.

Sorry, verbose is not the word I want... I don't know what word I
*actually* want, so let me explain.

Instead of thinking about a single transformation "change a list of
strings to a list of floats", the list comp form forces you to think
about the individual list items and the mechanics of looping, and reduces
lists to a second-class data type: we can operate on ints without caring
about individual bits, but we can't operate on lists without caring about
individual list items.

Why do I need to care about individual items? The list comp even gives
them a name, "x" in your example, even though that name isn't used
anywhere else. Why do I need to specify that walking the list must be
done from left-to-right rather than whatever order the compiler thinks
best, or even in parallel?

Answer: I don't, and shouldn't need to. With map, such internal details
of how the transformation is performed is hidden. I shouldn't need to
specify *how* to convert a list of strings to a list of floats. While a
list comp is less verbose than a for-loop, which is less again than a
while-loop, they all specify how to do the transformation, which is
mental overhead I shouldn't need to care about.

Of course, list comps are so seductively easy, and functional programming
so conceptually different from what many people are used to, that such
over-specification is an awfully easy trap to fall into. I'm sure my own
code is filled with similar examples where I use a list comp where a map
is more suitable.


--
Steven

Ben Finney

unread,
Sep 7, 2010, 12:20:43 AM9/7/10
to

Right. I find list comprehensions and generator expressions easy both to
write and to read.

So I prefer them, and often don't consider more function-based
approaches. I don't think my code (nor my examples) suffer much as a
result.

--
\ “Rightful liberty is unobstructed action, according to our |
`\ will, within limits drawn around us by the equal rights of |
_o__) others.” —Thomas Jefferson |
Ben Finney

nn

unread,
Sep 7, 2010, 10:52:22 AM9/7/10
to
On Sep 6, 10:31 pm, Steven D'Aprano <steve-REMOVE-

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

0 new messages