Yang Hui Triangle problem in Python Printing

print Yang Hui triangle, each line is a list, output with generator. I use two lists, fac, to represent the current line, and pre to represent the previous line. Why is it that when you change the current line in the for loop, the previous line is changed to the same as the current line?

Nov.25,2021

this scripting language stores non-basic type variables with "references" or "pointers". When you initialize an array, don't initialize one and then the other is directly equal to the first array, such as

.
a = [ 0 ] * n
b = a

this will cause two variables to point to the same memory address, and changing one will also change

. Learn about mutable and immutable objects in

python.

Menu