Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

If I can have honesty, it's easier to overlook mistakes. -- Kirk, "Space Seed", stardate 3141.9


devel / comp.lang.python / Re: code to initialize a sequence

SubjectAuthor
o Re: code to initialize a sequencePeter Otten

1
Re: code to initialize a sequence

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: __pete...@web.de (Peter Otten)
Newsgroups: comp.lang.python
Subject: Re: code to initialize a sequence
Date: Sun, 29 Aug 2021 21:15:03 +0200
Lines: 100
Message-ID: <mailman.498.1630265066.4164.python-list@python.org>
References: <CAN_spzhca96fw_sxO7mKRUdPQ6_t1vCEpWyqasffLniUT_OjBQ@mail.gmail.com>
<sggmt0$12qk$1@ciao.gmane.io>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de nAjJCREDHQtOsyIqfvLU0AtQtpqnaykLo7Ocu3QOwKfA==
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.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'def': 0.04; 'knows': 0.04;
'joseph': 0.05; 'random': 0.05; 'debug': 0.07; 'loop': 0.07;
'1600': 0.09; '797': 0.09; 'alternative': 0.09; 'identical': 0.09;
'message-id:@ciao.gmane.io': 0.09; 'numerical': 0.09;
'received:ciao.gmane.io': 0.09; 'received:gmane.io': 0.09;
'received:list': 0.09; 'import': 0.14; '+39': 0.16; '0644': 0.16;
'afterwards.': 0.16; 'exception.': 0.16; 'from:addr:web.de': 0.16;
'loops': 0.16; 'pareti': 0.16; "pareti's": 0.16;
'received:116.202': 0.16; 'received:116.202.254': 0.16;
'received:116.202.254.214': 0.16; 'subject:code': 0.16; 'turns':
0.16; 'url-ip:52.48/16': 0.16; 'url:joepareti54-ai': 0.16;
'wrote:': 0.16; 'python': 0.16; 'uses': 0.19; 'creates': 0.23;
'to:addr:python-list': 0.23; 'list,': 0.23; 'code': 0.24; 'skip:p
30': 0.26; 'length': 0.27; 'else': 0.27; 'bit': 0.28; 'sense':
0.28; 'header:User-Agent:1': 0.31; 'there': 0.31; 'code,': 0.31;
'but': 0.31; 'definitely': 0.31; 'expect': 0.31; 'item': 0.31;
'personally': 0.32; 'program': 0.33; 'work.': 0.33; 'header:In-
Reply-To:1': 0.33; 'same': 0.34; 'cell': 0.37; 'way': 0.37;
'read': 0.38; 'use': 0.38; 'learning': 0.38; 'does': 0.38; 'list':
0.39; 'include': 0.40; 'main': 0.40; 'skip:o 10': 0.62;
'services': 0.63; 'body': 0.63; 'consulting': 0.63; 'gives': 0.63;
'once': 0.64; 'becomes': 0.65; 'above': 0.65; 'attached': 0.68;
'received:116': 0.71; 'consultant': 0.74; 'easy': 0.75; 'garbage':
0.84; 'sequence.': 0.84; 'somebody': 0.91; 'sister': 0.93;
'turned': 0.95
X-Injected-Via-Gmane: http://gmane.org/
User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
In-Reply-To: <CAN_spzhca96fw_sxO7mKRUdPQ6_t1vCEpWyqasffLniUT_OjBQ@mail.gmail.com>
Content-Language: en-US
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: <sggmt0$12qk$1@ciao.gmane.io>
X-Mailman-Original-References: <CAN_spzhca96fw_sxO7mKRUdPQ6_t1vCEpWyqasffLniUT_OjBQ@mail.gmail.com>
 by: Peter Otten - Sun, 29 Aug 2021 19:15 UTC

On 29/08/2021 20:44, joseph pareti wrote:
> In the code attached below, the A-variant is from somebody else who knows
> Python better than I. But I do not like to just use any code without having
> a grasp, specifically the line in* bold*, so I wrote the B-variant which
> gives the same results. The C-variant is identical to A and is there for
> verification: after resetting the seed I expect the same sequence. The
> D-variant is closer to the way I code, and it does not work.

So you do you want us to debug the _EXPLICIT version?
The assignment

> X[i] = [randint(0, n_unique-1)]

creates a list with one element and turns it into an item in the list X.
You don't want a list, you want the numerical value, the
straight-forward way to achieve that being

X[i] = randint(0, n_unique-1)

An alternative is to assign to a slice

X[i:i+1] = [randint(...)]

but that would only make sense if the right-hand-side list weren't
created in the line and ditched immediately afterwards.

>
>
> import random
> from random import randint, seed
>
> def generate_sequence(length, n_unique):
> *return [randint(0, n_unique-1) for k in range(length)]*

The above is the most pythonic of the three versions. Once you
understand how for loops with a list.append() are turned into
comprehensions it will be easy to write and read that style. Definitely
worth learning and adopting.

>
> def generate_sequence_JP(length, n_unique):
> LI = []
> for k in range(length):
> LI.append(randint(0, n_unique-1))
> return(LI)

This is also fine and often used when the loop body is a bit more
complex, but

> def generate_sequence_EXPLICIT(length, n_unique):
> X =[None] * length
> for i in range(length):
> X[i] = [randint(0, n_unique-1)]
> return X

this is garbage even when it works, usually indicative of premature
optimization.

Random (yeah!) remark: Python uses half-open intervals (i. e. intervals
that include the lower bound, but not the upper bound) almost
everywhere. randint() is one exception.
Personally I prefer its conformist sister randrange(); with that
randint(0, n_unique-1)
becomes
randrange(n_unique)

> #
> # MAIN PROGRAM
> #
> random.seed(2)
> A = generate_sequence(4, 10 )
> random.seed(2)
> B = generate_sequence_JP(4, 10)
> random.seed(2)
> C = generate_sequence(4, 10 )
> random.seed(2)
> D = generate_sequence_EXPLICIT(4, 10 )
> print(A)
> print(type(A))
> print('-----------------------------')
> print(B)
> print(type(B))
> print('-----------------------------')
> print(C)
> print(type(C))
> print('-----------------------------')
> print(D)
> print(type(D))
>
>
> Regards,
> Joseph Pareti - Artificial Intelligence consultant
> Joseph Pareti's AI Consulting Services
> https://www.joepareti54-ai.com/
> cell +49 1520 1600 209
> cell +39 339 797 0644
>

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor