Sep-07-2017, 03:28 AM
Hi :)
I want to generate a list of lists of the form:
Yes I'm a bit angry because I just lost one hour to understand why my monte-carlo optimization of non-convex stuff did send me errors it shouldn't. It's easy, I don't have the starting list of indices I should have.
I want to generate a list of lists of the form:
[ [0, 4, 4, 4, 4] [4, 0, 4, 4, 4] [4, 4, 0, 4, 4] [4, 4, 4, 0, 4] [4, 4, 4, 4, 0] ]Nothing I'm trying works, it seems python wants to simplify my life by changing all the lists indices instead of just the one i'm pointing at him. Examples:
Xs = [[4]*5]*5 for i, X in enumerate(Xs): Xs[i][i] = 0 print Xs Xs, t = [], [4, 4, 4, 4, 4] for i in range(5): Xs.append(t) Xs[i][i] = 0 print Xs Xs, t = [], [4, 4, 4, 4, 4] for i in range(5): temp = t temp[i] = 0 Xs.append(temp) print Xs print t, "f you, i don't even touch t"outputs:
Output:[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
[0, 0, 0, 0, 0] f you, i don't even touch t
In reality, I need to make a slightly bigger list, and I could totally make it by hand, but seriously what is this behaviour? Not only it's totally unintuitive, but especially the last example is ridiculous, I just can't find how to generate that... Is my logic flawed there and I'm too blind to see?Yes I'm a bit angry because I just lost one hour to understand why my monte-carlo optimization of non-convex stuff did send me errors it shouldn't. It's easy, I don't have the starting list of indices I should have.