Deutsch   English   Français   Italiano  
<61af2e22$0$28605$426a34cc@news.free.fr>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder6.news.weretis.net!feeder8.news.weretis.net!news.trigofacile.com!usenet-fr.net!agneau.org!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!212.27.60.64.MISMATCH!cleanfeed3-b.proxad.net!nnrp1-2.free.fr!not-for-mail
Date: Tue, 7 Dec 2021 10:49:21 +0100
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101
 Thunderbird/91.3.2
Newsgroups: fr.comp.lang.python
Content-Language: fr
From: ast <ast@invalid>
Subject: =?UTF-8?Q?Un_code_que_j=27ai_mis_du_temps_=c3=a0_comprendre?=
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 29
Message-ID: <61af2e22$0$28605$426a34cc@news.free.fr>
Organization: Guest of ProXad - France
NNTP-Posting-Date: 07 Dec 2021 10:49:22 CET
NNTP-Posting-Host: 91.170.32.5
X-Trace: 1638870562 news-4.free.fr 28605 91.170.32.5:3315
X-Complaints-To: abuse@proxad.net
Bytes: 1633

Trouvez ce que fait le code suivant ?
(sans l'éxécuter bien sur)


 >>> n = 3
 >>> x = range(n ** 2),
 >>> xn = list(zip(*[iter(x)] * n))
 >>> xn


Réponse:

Tout d'abord dans le *[iter(x)] * n c'est la
2ème étoile (multiplication) qui est prioritaire
sur la 1ère (splash), ça n'a rien d'évident

On se retrouve donc avec:

[iter(x), iter(x), iter(x)]

mais c'est le même objet itérateur qui est répété
3 fois, équivalent à it = iter(x) puis [it, it, it]

après list(zip(it, it, it))


-> [(0, 1, 2), (3, 4, 5), (6, 7, 8)]

la fonction réalisée est donc un "tronçonnage" de x