I'm a little confused about how python handles a link to an item in a list, given 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 the first example, the contents in list a are changed; while in the second example, the contents of the list a are not changed. Why is this?
source
share