Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

"The only way for a reporter to look at a politician is down." -- H. L. Mencken


devel / comp.lang.python / Re: round decimal values

SubjectAuthor
* round decimal valuesjo
+- Re: round decimal valuesStefan Ram
`* Re: round decimal valuesJulio Di Egidio
 `* Re: round decimal valuesjo
  `- Re: round decimal valuesDennis Lee Bieber

1
round decimal values

<sb24g7$1cp0$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=18302&group=comp.lang.python#18302

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!aioe.org!/VJ1K9Z+Rg5cOQYojh+dlQ.user.gioia.aioe.org.POSTED!not-for-mail
From: jo@email.com (jo)
Newsgroups: comp.lang.python
Subject: round decimal values
Date: Thu, 24 Jun 2021 14:23:03 +0000 (UTC)
Organization: Aioe.org NNTP Server
Lines: 20
Message-ID: <sb24g7$1cp0$1@gioia.aioe.org>
NNTP-Posting-Host: /VJ1K9Z+Rg5cOQYojh+dlQ.user.gioia.aioe.org
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Complaints-To: abuse@aioe.org
User-Agent: Pan/0.146 (Hic habitat felicitas; 8107378
git@gitlab.gnome.org:GNOME/pan.git)
X-Notice: Filtered by postfilter v. 0.9.2
 by: jo - Thu, 24 Jun 2021 14:23 UTC

Hi,

this code generate 4 values with gaussian distribution (mu=5, sigma=1):

import numpy as np
x = np.random.normal(5, 1, 4)
print(x)

Output:
[5.87879753 3.29162433 3.83024698 4.92997148]

I would like to round the output like this:

[6, 3, 4, 5]

What should I add to the code?

Thank you

j.

Re: round decimal values

<map-round-20210624162247@ram.dialup.fu-berlin.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=18303&group=comp.lang.python#18303

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: round decimal values
Date: 24 Jun 2021 15:28:09 GMT
Organization: Stefan Ram
Lines: 13
Expires: 1 Sep 2021 11:59:58 GMT
Message-ID: <map-round-20210624162247@ram.dialup.fu-berlin.de>
References: <sb24g7$1cp0$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de lWyBZhMOFu9lagtxuVJUHglHBWbdnJpoDcCvnFo/OV70FW
X-Copyright: (C) Copyright 2021 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Thu, 24 Jun 2021 15:28 UTC

jo <jo@email.com> writes:
>I would like to round the output like this:
>[6, 3, 4, 5]
>What should I add to the code?

We are not supposed to do anything here that might be
homework, but maybe I am allowed to say that Python has
several facilites that allow one to map functions to entries
of a list (for example, higher-order map functions and "for"
comprehensions) and then convert the resulting iterable
object back to a list.

Re: round decimal values

<sb2ajb$dfl$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=18305&group=comp.lang.python#18305

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: julio@diegidio.name (Julio Di Egidio)
Newsgroups: comp.lang.python
Subject: Re: round decimal values
Date: Thu, 24 Jun 2021 18:07:07 +0200
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <sb2ajb$dfl$1@dont-email.me>
References: <sb24g7$1cp0$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 24 Jun 2021 16:07:07 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="c0a5679652089bbacd0fa88a1f46ada2";
logging-data="13813"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19UTV81DzJBk6nr6itrbwsTpPABN0pTwEQ="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:Lik1VMQ+8ef2QvUjN/qOmjXZ7Tw=
In-Reply-To: <sb24g7$1cp0$1@gioia.aioe.org>
Content-Language: en-GB
 by: Julio Di Egidio - Thu, 24 Jun 2021 16:07 UTC

On 24/06/2021 16:23, jo wrote:
> Hi,
>
> this code generate 4 values with gaussian distribution (mu=5, sigma=1):
>
> import numpy as np
> x = np.random.normal(5, 1, 4)
> print(x)
>
> Output:
> [5.87879753 3.29162433 3.83024698 4.92997148]
>
> I would like to round the output like this:
>
> [6, 3, 4, 5]
>
> What should I add to the code?
>
> Thank you

Have a look at numpy.around:
<https://numpy.org/doc/stable/reference/generated/numpy.around.html>

HTH,

Julio

Re: round decimal values

<sb2bqe$5l5$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=18306&group=comp.lang.python#18306

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!aioe.org!3mnkKgLY8NY+b4ISmGNhsg.user.gioia.aioe.org.POSTED!not-for-mail
From: jo@email.com (jo)
Newsgroups: comp.lang.python
Subject: Re: round decimal values
Date: Thu, 24 Jun 2021 16:27:58 +0000 (UTC)
Organization: Aioe.org NNTP Server
Lines: 52
Message-ID: <sb2bqe$5l5$1@gioia.aioe.org>
References: <sb24g7$1cp0$1@gioia.aioe.org> <sb2ajb$dfl$1@dont-email.me>
NNTP-Posting-Host: 3mnkKgLY8NY+b4ISmGNhsg.user.gioia.aioe.org
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Complaints-To: abuse@aioe.org
User-Agent: Pan/0.146 (Hic habitat felicitas; 8107378
git@gitlab.gnome.org:GNOME/pan.git)
X-Notice: Filtered by postfilter v. 0.9.2
 by: jo - Thu, 24 Jun 2021 16:27 UTC

Il Thu, 24 Jun 2021 18:07:07 +0200, Julio Di Egidio ha scritto:

> On 24/06/2021 16:23, jo wrote:
>> Hi,
>>
>> this code generate 4 values with gaussian distribution (mu=5, sigma=1):
>>
>> import numpy as np x = np.random.normal(5, 1, 4)
>> print(x)
>>
>> Output:
>> [5.87879753 3.29162433 3.83024698 4.92997148]
>>
>> I would like to round the output like this:
>>
>> [6, 3, 4, 5]
>>
>> What should I add to the code?
>>
>> Thank you
>
> Have a look at numpy.around:
> <https://numpy.org/doc/stable/reference/generated/numpy.around.html>
>
> HTH,
>
> Julio

Yes, np.around() works fine.
Thank you.

This is the new code:

import numpy as np
x = np.random.normal(5, 1, 4)

y=np.around(x)

print(x)

print(y)

output:

[4.68551569 3.9610641 6.1119447 4.81012246]
[5. 4. 6. 5.]

Thank you very much,

j.

Re: round decimal values

<mailman.167.1624758047.4164.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=18339&group=comp.lang.python#18339

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: wlfraed@ix.netcom.com (Dennis Lee Bieber)
Newsgroups: comp.lang.python
Subject: Re: round decimal values
Date: Fri, 25 Jun 2021 12:05:26 -0400
Organization: IISS Elusive Unicorn
Lines: 70
Message-ID: <mailman.167.1624758047.4164.python-list@python.org>
References: <sb24g7$1cp0$1@gioia.aioe.org> <sb2ajb$dfl$1@dont-email.me>
<sb2bqe$5l5$1@gioia.aioe.org>
<0iubdgt2guamj2uli1c617vjsfeq4qh496@4ax.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de PTET/BKjR+3vAY7blP4K9grUuWwdoxAiVZR2Jj24WmUw==
Return-Path: <python-python-list@m.gmane-mx.org>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'library.': 0.05; 'random':
0.05; 'string': 0.05; 'math': 0.07; 'numpy': 0.09;
'received:ciao.gmane.io': 0.09; 'received:gmane.io': 0.09;
'received:list': 0.09; 'import': 0.14; 'importing': 0.16;
'integer': 0.16; 'message-id:@4ax.com': 0.16; 'numpy,': 0.16;
'received:116.202': 0.16; 'received:116.202.254': 0.16;
'received:116.202.254.214': 0.16; 'skip:> 10': 0.16; 'strings,':
0.16; 'subject:values': 0.16; 'values': 0.16; 'python': 0.16;
'thu,': 0.20; 'to:addr:python-list': 0.23; '>>>': 0.26; 'seems':
0.26; 'jun': 0.27; 'library': 0.27; 'text': 0.29; 'header:User-
Agent:1': 0.31; 'header:Organization:1': 0.31; 'well,': 0.35;
'code:': 0.35; 'those': 0.38; 'url-ip:151.101.36.223/32': 0.38;
'url-ip:151.101.36/24': 0.38; 'your': 0.64; 'skip:r 20': 0.64;
'received:116': 0.71; 'formatting': 0.77; '....': 0.81; '2021':
0.84; 'massive': 0.84; 'url-ip:76/8': 0.84
X-Injected-Via-Gmane: http://gmane.org/
User-Agent: ForteAgent/8.00.32.1272
X-No-Archive: YES
X-Mailman-Approved-At: Sat, 26 Jun 2021 21:40:45 -0400
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.34
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <0iubdgt2guamj2uli1c617vjsfeq4qh496@4ax.com>
X-Mailman-Original-References: <sb24g7$1cp0$1@gioia.aioe.org>
<sb2ajb$dfl$1@dont-email.me> <sb2bqe$5l5$1@gioia.aioe.org>
 by: Dennis Lee Bieber - Fri, 25 Jun 2021 16:05 UTC

On Thu, 24 Jun 2021 16:27:58 +0000 (UTC), jo <jo@email.com> declaimed the
following:

>This is the new code:
>
>import numpy as np
>
>x = np.random.normal(5, 1, 4)
>
>y=np.around(x)
>
>print(x)
>
>print(y)
>
>
>output:
>
>[4.68551569 3.9610641 6.1119447 4.81012246]
>[5. 4. 6. 5.]

Ouch... importing the massive numpy library just to do rounding? Well,
no -- you did import it to generate your data... Still seems a big library
for such a small assignment.

Without numpy, you might want to explore the Python standard library.
https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex
https://docs.python.org/3/library/random.html#real-valued-distributions

Consider:
int(), round(), math.trunc(), math.floor(), math.ceil()

random.gauss(), random.normalvariate()

>>> import math
>>> import random
>>> x = [random.gauss(5, 1) for _ in range(4)]
>>> x
[4.679373663118109, 6.523100884104224, 5.320871577393969,
3.6448059793115313]
>>> yint = [int(v) for v in x]
>>> yint
[4, 6, 5, 3]
>>> yround = [round(v) for v in x]
>>> yround
[5, 7, 5, 4]
>>>

If you only need the integer values for output, then string formatting
can suffice...

>>> for v in x:
.... print("%d" % v, end="\t")
....
4 6 5 3
>>> for v in x:
.... print("%d" % (v+0.5), end="\t")
....
5 7 5 4
>>>

Note that those are TEXT strings, not integers, at point of output.

--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/


devel / comp.lang.python / Re: round decimal values

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor