0

For loop assigning last value of list to entire 2D list.

I have this code:

for i in range(len(horiz_lines)):
  for j in range(len(vert_lines)):
    resultant = intersection(horiz_boxes[horiz_lines[i]], vert_boxes[vert_lines[ordered_boxes[j]]])

    for b in range(len(boxes)):
      the_box = [boxes[b][0][0],boxes[b][0][1],boxes[b][2][0],boxes[b][2][1]]
      if iou(resultant,the_box)>0.1:
        out_array[i][j]=texts[b]
        print(out_array[i][j])

Now, functions are working fine, when I print out_array inside loop (as given in code), I get expected values. But once I call out_array in next cell and print it, values are changed. What mistake am I making?

Edit: Here is how out_array was created.

out_array = [["" for i in range(len(vert_lines))] for j in range(len(horiz_lines))]
  • 1
    One reason could be that `texts[b]` changed. You haven't shown us that variable, or how it's used, so we can't know for sure. – John Gordon May 22 '23 at 03:49
  • @JohnGordon I checked `texts[b]` by printing, it is what it is supposed to be. Also, it is simple list. – siddharth patel May 22 '23 at 03:50
  • There are too many unknowns here to say anything with certainty. Having said that — how is `out_array` created? I suspect something similar to [this issue](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly). – Ture Pålsson May 22 '23 at 03:50
  • @TurePålsson I made edit for out_array. Thanks. – siddharth patel May 22 '23 at 03:52
  • 1
    You're aware that `out_array[i][j]=texts[b]` might be executed many times for the same values of `[i][j]` but different values of `b`? – John Gordon May 22 '23 at 03:55
  • 1
    What do you mean by "once I call out_array in next cell and print it"? What do you mean by _call_ exactly? Do you mean call as in _call a function_? And what do you mean by "next cell"? – John Gordon May 22 '23 at 03:58
  • @JohnGordon I am not sure about that. Any way by which I can check this? Btw, `len(horiz_lines)` is 29, `len(vert_lines)` is 5 and `len(texts)` is 55. – siddharth patel May 22 '23 at 03:58
  • @JohnGordon by next cell, I mean I am running in Jupyter notebook. So after running this loop part, I print out_array in next cell. – siddharth patel May 22 '23 at 03:59
  • 1
    What is a "cell" exactly? Is it one line of code? – John Gordon May 22 '23 at 04:00
  • 1
    It would help a lot if you showed us the **exact** output that changed. – John Gordon May 22 '23 at 04:02
  • 1
    Do you expect `iou(resultant,the_box)>0.1` to be true only once for a given `[i][j]`? – John Gordon May 22 '23 at 04:05
  • @JohnGordon yes, it should be true only once, also cell is block of code in Jupyter notebook. – siddharth patel May 22 '23 at 04:07
  • 1
    Then perhaps the problem is that `iou(resultant,the_box)>0.1` is actually true more than once for a given `[i][j]`. You can check this yourself by adding a simple print statement inside the if, something like `print(f'out_array[{i}][{j}]=texts[{b}]')` and see if there are two (or more) messages with different values of b for the same values of i and j. – John Gordon May 22 '23 at 04:12
  • 1
    Or, simpler, just print `i` and `j`, and see if the same values are ever printed twice. – John Gordon May 22 '23 at 04:16
  • @JohnGordon i, j for multiple times was case, I changed things in iou function by bit and everything working. Thanks a lot! – siddharth patel May 22 '23 at 04:38
  • 1
    Get out of the habit of using `for index in range(len(list)):`. Use `for item in list:` or `for index, item in enumerate(list):` – Barmar May 22 '23 at 06:00

0 Answers0