[Linux-bruxelles] um mystère pythonien

Didrik Pinte dpinte at itae.be
Ven 23 Juin 12:43:32 CEST 2006


On Fri, 2006-06-23 at 12:26 +0200, Marc Van Craesbeeck wrote:
> Bonjour la liste
> 
> Quelqu'un peut-il m'expliquer ce "mystère" en python:
> 
> 	liste1=['a']
> 	liste2=[]
> 	parametre=['b']
> 	for x in range(2):
> 		liste2.append(liste1)
> 		liste2[x].insert(x,parametre[0])
> 
> Il me sort pour liste2: 
> 
> 	[['b', 'b', 'a'], ['b', 'b', 'a']]

Logique, si tu décomposes ta boucle étape par étape :

x = 0
append liste2 = [['a']]
insert liste2 = [['b', 'a']]
x = 1
append liste2 = [['b', 'a'], ['b', 'a']]
insert liste2 = [['b', 'b', 'a'], ['b', 'b', 'a']]

--
Didrik
-------------- section suivante --------------
Une pièce jointe autre que texte a été nettoyée...
Nom: signature.asc
Type: application/pgp-signature
Taille: 189 octets
Desc: This is a digitally signed message part
URL: </pipermail/linux-bruxelles/attachments/20060623/8eace968/attachment-0001.sig>


Plus d'informations sur la liste de diffusion Linux-bruxelles