mercredi 7 septembre 2016

Reference to an element in a list

I am a little confused about how python deal with reference to an element in a list, considering these two examples:

First example:

import random
a = [[1,2],[3,4],[5,6],[7,8]]
b = [0.1,0.2]
c = random.choice(a)
c[:] = b
print(a)

Second example:

import random
a = [1, 2, 3, 4, 5, 6, 7, 8]
b = 0.1
c = random.choice(a)
c = b
print(a)

In first example, the content in list a is changed; while in the second example, the content of list a is not changed. Why is that?




Aucun commentaire:

Enregistrer un commentaire