0

I stumbled upon something that for the time being looks really weird to me. Maybe i have been looking at it for too long and too up-close, so i am counting on your fresh pair of eyes and, of course, expertise.

I have a list that i want to update, namely ply_info. The update consists of getting an element from the list (the one pointed by sign_reversal_index), duplicating it and adjusting the values of the original and copy-cat entries. This final bit is the weird one and takes place at the two last lines of code.

ply_info = [[0.3], [0.13], [0.13], [0.3], [0.13], [0.13], [0.3], [0.3], [0.3], [0.3], [0.3], [0.3], [0.3], [0.3], [0.3], [0.3], [0.3]]
t_ply = [0.3, 0.13, 0.13, 0.3, 0.13, 0.13, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3]

height_list = [0]
for i in range(len(t_ply)):
    height_list.append(height_list[i] + t_ply[i])

mid_plane_z = sum(t_ply) / 2

if mid_plane_z not in height_list:
    print("...No ply boundary lies on mid plane.\n   ...Splitting the one that crosses it.\n")
    height_list.append(mid_plane_z)
    height_list = sorted(height_list)
    sign_reversal_index = height_list.index(mid_plane_z)
    ply_to_duplicate = ply_info[sign_reversal_index-1]
    ply_info.insert(sign_reversal_index-1, ply_to_duplicate)
    ply_info[sign_reversal_index-1][0] = height_list[sign_reversal_index] - height_list[sign_reversal_index-1]
    ply_info[sign_reversal_index][0] = height_list[sign_reversal_index+1] - height_list[sign_reversal_index]

I debugged the thing and it looks like the two entries are somehow linked and updating the one, updates the other too. Both values get updated twice, once in the second to last and once in the last line of code. Can anybody please explain why something like that is happening and how it relates to the creation of the copy? what would be the way to go about\around this?

the correct output would be:

[[0.3], [0.13], [0.13], [0.3], [0.13], [0.13], [0.3], [0.3], [0.3], [0.19], [0.11], [0.3], [0.3], [0.3], [0.3], [0.3], [0.3], [0.3]]

notice the ...,[0.19], [0.11],...

Instead I am getting ...,[0.11], [0.11],...

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ma0
  • 15,057
  • 4
  • 35
  • 65
  • just a guess, but... negative index? anyways, just print all the relevant data and you'll see what's causing it. – Karoly Horvath Jun 02 '16 at 13:40
  • nope. the index is positive and reflects the position of the "mid_plane_z" in in the "height_list" after it is sorted. i mean the list.index() method might be based on something as dodgy as floating point value comparisons but as i said, i debugged it and the index value is 10.. – Ma0 Jun 02 '16 at 13:55
  • 1
    I can't really grok the code you posted to an extent that I can say exactly where this is happening, but if you've got updates to one list propagating to another then the problem is you haven't got two lists. `a = b` in Python where `b` is a list gives the list `b` a second name. `a = b[:]` (or other methods) makes a copy. – Two-Bit Alchemist Jun 02 '16 at 13:55
  • @Two-BitAlchemist: that's exactly what the problem was. I fail to grasp this though. I mean does this only happen with lists? Whats the purpose of it? Saving memory ? – Ma0 Jun 02 '16 at 13:58
  • @Ev.Kounis No it also happens with dicts, sets, and other large mutable objects. It's a performance consideration so that the potentially large structure is not copied unnecessarily and that control of when it is copied belongs to the programmer and not the Python language implementation. If you're familiar with C, it's pretty much just passing a pointer in the background. If you're not familiar with C, ignore that sentence if it's just confusing. :) – Two-Bit Alchemist Jun 02 '16 at 14:01

0 Answers0