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


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to