I am doing a problem, like word ladder.
Originally I have a string list: paths = [[A]]
.
Now B, C and D are all strings which have only one different letter compared
to A. What I wish to do is to make
[[A]] with [B,C,D] -> paths = [[A,B], [A,C], [A,D]]
But if I copy the [[A]]
threes tames to prepare paths = [[A], [A], [A]]
using paths = [paths[0]]*3
, then then doing:
i=0
for word in [B,C,D]:
newpath = paths[i]
paths.append(newpath.append(word))
i += 1
which shows [[A, B, C, D], [A, B, C, D], [A, B, C, D]]
. It seems the same operation on each copy. Even though I used paths = [paths[0] for i in range(3)]
, it is still the same. How can I avoid it? What's wrong with my code?
A ton of thanks!