Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41603 > unrolled thread

Lists and Decimal numbers

Started byAna Dionísio <anadionisio257@gmail.com>
First post2013-03-20 08:27 -0700
Last post2013-03-20 20:47 +0000
Articles 7 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Lists and Decimal numbers Ana Dionísio <anadionisio257@gmail.com> - 2013-03-20 08:27 -0700
    Re: Lists and Decimal numbers Wanderer <wanderer@dialup4less.com> - 2013-03-20 08:48 -0700
    Re: Lists and Decimal numbers Peter Otten <__peter__@web.de> - 2013-03-20 16:52 +0100
      Re: Lists and Decimal numbers Alister <alister.ware@ntlworld.com> - 2013-03-20 19:20 +0000
        Re: Lists and Decimal numbers Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-03-20 19:53 +0000
        Re: Lists and Decimal numbers Grant Edwards <invalid@invalid.invalid> - 2013-03-20 20:00 +0000
          Re: Lists and Decimal numbers Alister <alister.ware@ntlworld.com> - 2013-03-20 20:47 +0000

#41603 — Lists and Decimal numbers

FromAna Dionísio <anadionisio257@gmail.com>
Date2013-03-20 08:27 -0700
SubjectLists and Decimal numbers
Message-ID<9d8e4386-e6f5-4303-8a6a-be2a16e45ce7@googlegroups.com>
So, I have this script that puts in a list every minute in 24 hours

hour=[]
i=0
t=-(1.0/60.0)
while i<24*60:
    i = i+1
    t = t+(1.0/60.0)
    hour.append([t])

When it is doing the cicle it can have all the decimal numbers, but I need to print the result with only 4 decimal numbers

How can I define the number of decimal numbers I want to print in this case? For example with 4 decimal numbers, it would print:

0.0000
0.0167
0.0333
...

Can you help?

[toc] | [next] | [standalone]


#41604

FromWanderer <wanderer@dialup4less.com>
Date2013-03-20 08:48 -0700
Message-ID<0180bf86-4d01-4840-9284-42cf4976b685@googlegroups.com>
In reply to#41603
On Wednesday, March 20, 2013 11:27:30 AM UTC-4, Ana Dionísio wrote:
> So, I have this script that puts in a list every minute in 24 hours
> 
> 
> 
> hour=[]
> 
> i=0
> 
> t=-(1.0/60.0)
> 
> while i<24*60:
> 
>     i = i+1
> 
>     t = t+(1.0/60.0)
> 
>     hour.append([t])
> 
> 
> 
> When it is doing the cicle it can have all the decimal numbers, but I need to print the result with only 4 decimal numbers
> 
> 
> 
> How can I define the number of decimal numbers I want to print in this case? For example with 4 decimal numbers, it would print:
> 
> 
> 
> 0.0000
> 
> 0.0167
> 
> 0.0333
> 
> ...
> 
> 
> 
> Can you help?

You can use round.

for t in hour:
    print round(t,4)

[toc] | [prev] | [next] | [standalone]


#41605

FromPeter Otten <__peter__@web.de>
Date2013-03-20 16:52 +0100
Message-ID<mailman.3567.1363794723.2939.python-list@python.org>
In reply to#41603
Ana Dionísio wrote:

> So, I have this script that puts in a list every minute in 24 hours
> 
> hour=[]
> i=0
> t=-(1.0/60.0)
> while i<24*60:
>     i = i+1
>     t = t+(1.0/60.0)
>     hour.append([t])

In many cases you can write

for i in range(...):
   ...

instead of incrementing manually.
 
> When it is doing the cicle it can have all the decimal numbers, but I need
> to print the result with only 4 decimal numbers
> 
> How can I define the number of decimal numbers I want to print in this
> case? For example with 4 decimal numbers, it would print:
> 
> 0.0000
> 0.0167
> 0.0333
> ...
> 
> Can you help?


>>> for i in range(24*60):
...     print "{:.4f}".format(i/60.0)
... 
0.0000
0.0167
0.0333
0.0500
[...]
23.9500
23.9667
23.9833
>>> 

See also

<http://docs.python.org/2/library/string.html#format-specification-mini-language>

[toc] | [prev] | [next] | [standalone]


#41617

FromAlister <alister.ware@ntlworld.com>
Date2013-03-20 19:20 +0000
Message-ID<t_n2t.145340$927.82085@fx23.fr7>
In reply to#41605
On Wed, 20 Mar 2013 16:52:00 +0100, Peter Otten wrote:

> Ana Dionísio wrote:
> 
>> So, I have this script that puts in a list every minute in 24 hours
>> 
>> hour=[]
>> i=0 t=-(1.0/60.0)
>> while i<24*60:
>>     i = i+1 t = t+(1.0/60.0)
>>     hour.append([t])
> 
> In many cases you can write
> 
> for i in range(...):
>    ...
> 
> instead of incrementing manually.
>  
>> When it is doing the cicle it can have all the decimal numbers, but I
>> need to print the result with only 4 decimal numbers
>> 
>> How can I define the number of decimal numbers I want to print in this
>> case? For example with 4 decimal numbers, it would print:
>> 
>> 0.0000 0.0167 0.0333 ...
>> 
>> Can you help?
> 
> 
>>>> for i in range(24*60):
> ...     print "{:.4f}".format(i/60.0)
> ...
> 0.0000 0.0167 0.0333 0.0500 [...]
> 23.9500 23.9667 23.9833
>>>> 
>>>> 
> See also
> 
> <http://docs.python.org/2/library/string.html#format-specification-mini-
language>


and a list comprehension would streamline things further

t=[round(x*1.0/60),4 for x in range(1440)] #compatible with V2.7 & V3.0)

-- 
How many Zen Buddhist does it take to change a light bulb?

Two.  One to change it and one not to change it.

[toc] | [prev] | [next] | [standalone]


#41619

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-03-20 19:53 +0000
Message-ID<mailman.3575.1363809193.2939.python-list@python.org>
In reply to#41617
On 20/03/2013 19:20, Alister wrote:
> On Wed, 20 Mar 2013 16:52:00 +0100, Peter Otten wrote:
>
>> Ana Dionísio wrote:
>>
>>> So, I have this script that puts in a list every minute in 24 hours
>>>
>>> hour=[]
>>> i=0 t=-(1.0/60.0)
>>> while i<24*60:
>>>      i = i+1 t = t+(1.0/60.0)
>>>      hour.append([t])
>>
>> In many cases you can write
>>
>> for i in range(...):
>>     ...
>>
>> instead of incrementing manually.
>>
>>> When it is doing the cicle it can have all the decimal numbers, but I
>>> need to print the result with only 4 decimal numbers
>>>
>>> How can I define the number of decimal numbers I want to print in this
>>> case? For example with 4 decimal numbers, it would print:
>>>
>>> 0.0000 0.0167 0.0333 ...
>>>
>>> Can you help?
>>
>>
>>>>> for i in range(24*60):
>> ...     print "{:.4f}".format(i/60.0)
>> ...
>> 0.0000 0.0167 0.0333 0.0500 [...]
>> 23.9500 23.9667 23.9833
>>>>>
>>>>>
>> See also
>>
>> <http://docs.python.org/2/library/string.html#format-specification-mini-
> language>
>
>
> and a list comprehension would streamline things further
>
> t=[round(x*1.0/60),4 for x in range(1440)] #compatible with V2.7 & V3.0)
>

Really?

c:\Users\Mark\Python>python
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 
bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> t=[round(x*1.0/60),4 for x in range(1440)] #compatible with V2.7 & 
V3.0)
   File "<stdin>", line 1
     t=[round(x*1.0/60),4 for x in range(1440)] #compatible with V2.7 & 
V3.0)
                            ^
SyntaxError: invalid syntax

-- 
Cheers.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#41620

FromGrant Edwards <invalid@invalid.invalid>
Date2013-03-20 20:00 +0000
Message-ID<kid4h6$1hv$1@reader2.panix.com>
In reply to#41617
On 2013-03-20, Alister <alister.ware@ntlworld.com> wrote:

> and a list comprehension would streamline things further
>
> t=[round(x*1.0/60),4 for x in range(1440)] #compatible with V2.7 & V3.0)

There's a typo in the above.  It should be:

 t = [round((x*1.0/60),4) for x in range(1440)]

-- 
Grant Edwards               grant.b.edwards        Yow! On the road, ZIPPY
                                  at               is a pinhead without a
                              gmail.com            purpose, but never without
                                                   a POINT.

[toc] | [prev] | [next] | [standalone]


#41622

FromAlister <alister.ware@ntlworld.com>
Date2013-03-20 20:47 +0000
Message-ID<Afp2t.202953$VO5.3067@fx20.fr7>
In reply to#41620
On Wed, 20 Mar 2013 20:00:38 +0000, Grant Edwards wrote:

> On 2013-03-20, Alister <alister.ware@ntlworld.com> wrote:
> 
>> and a list comprehension would streamline things further
>>
>> t=[round(x*1.0/60),4 for x in range(1440)] #compatible with V2.7 &
>> V3.0)
> 
> There's a typo in the above.  It should be:
> 
>  t = [round((x*1.0/60),4) for x in range(1440)]

Indeed a typo
Many thanks for correcting



-- 
"MacDonald has the gift on compressing the largest amount of words into
the smallest amount of thoughts."
		-- Winston Churchill

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web