0

Okay, maybe this question look stupid. Or maybe I don't know how it's work because I was a beginner in Python.

So, I just discovered this things. And it makes no sense for me. First thing, Sorry for my bad English. I hope you get understand what I told about. So, I want to make a function that return array of all combination length x width from the area of some squares. Let's assumed that function called listCombination.

Example: we have an array that told area of some squares called arrArea = [ 2, 3, 4 ]

so if I called listCombination(arrArea), it's should return like this [[[1, 2], [2, 1]], [[1, 3], [3, 1]], [[1, 4], [2, 2], [4, 1]]]

Here is my code :

array = [2, 3, 4]
ex = []
ex2 = []
for i in array:
    a = factor(i)
    rA = a[::-1]
    for j in range(len(a)):
        ex.append([a[j], rA[j]])
    print("Array ex:")
    print(ex)
    ex2.append(ex)
    ex.clear()

print("\nArray ex2:", ex2)

I was really sure that my code was already right. But, the output show nothing in array ex2

Here is the output :

Array ex:
[[1, 2], [2, 1]]
Array ex:
[[1, 3], [3, 1]]
Array ex:
[[1, 4], [2, 2], [4, 1]]

Array ex2: [[], [], []]

I was confused, array ex was already right. and every loop (before I cleared array ex) I've already add ex to ex2 using append. So, why ex2 still have an empty array? I was wondering and try to add print(ex2) in my code to make sure that ex2.append(ex) really work

So, this is the second code :

array = [2, 3, 4]
ex = []
ex2 = []
for i in array:
    a = factor(i)
    rA = a[::-1]
    for j in range(len(a)):
        ex.append([a[j], rA[j]])
    print("Array ex:")
    print(ex)
    ex2.append(ex)
    print("\nArray ex2:")
    print(ex2)
    print("\n")
    ex.clear()

And this is the second output :

Array ex:
[[1, 2], [2, 1]]

Array ex2:
[[[1, 2], [2, 1]]]

Array ex:
[[1, 3], [3, 1]]

Array ex2:
[[[1, 3], [3, 1]], [[1, 3], [3, 1]]]

Array ex:
[[1, 4], [2, 2], [4, 1]]

Array ex2:
[[[1, 4], [2, 2], [4, 1]], [[1, 4], [2, 2], [4, 1]], [[1, 4], [2, 2], [4, 1]]]

So, array ex2 not really empty. I am not sure about this but I think, this ex.clear() line of code somehow makes ex2 empty. Beside that problem, why ex2 in the second and third loop have value "[[[1, 3], [3, 1]], [[1, 3], [3, 1]]]" and "[[[1, 4], [2, 2], [4, 1]], [[1, 4], [2, 2], [4, 1]], [[1, 4], [2, 2], [4, 1]]]"? doesn't it should be "[[[1, 2], [2, 1]], [[1, 3], [3, 1]]]" and "[[[1, 2], [2, 1]], [[1, 3], [3, 1]], [[1, 4], [2, 2], [4, 1]]]"?

And I make some change my code like this (I add ex2.clear() in the last line) :

array = [2, 3, 4]
ex = []
ex2 = []
for i in array:
    a = factor(i)
    rA = a[::-1]
    for j in range(len(a)):
        ex.append([a[j], rA[j]])
    print("Array ex:")
    print(ex)
    ex2.append(ex)
    print("\nArray ex2:")
    print(ex2)
    print("\n")
    ex.clear()
    ex2.clear()

And the third output is :

Array ex:
[[1, 2], [2, 1]]

Array ex2:
[[[1, 2], [2, 1]]]

Array ex:
[[1, 3], [3, 1]]

Array ex2:
[[[1, 3], [3, 1]]]

Array ex:
[[1, 4], [2, 2], [4, 1]]

Array ex2:
[[[1, 4], [2, 2], [4, 1]]]

Can someone explain to me why this is happen? and what can I do, so the listCombination function return like what I want?

Thank you very much

  • You only have One. `ex`. List. Object. You’ll want to create a new list object in each loop iteration instead, and not clear it. – deceze Mar 16 '21 at 18:15
  • Note: In this case, copying isn't really necessary as in the dupe. Just move the `ex = []` line inside the loop (immediately after the `for`), and delete the `ex.clear()`. As is, `ex2` was a bunch of aliases to the same `list`, which you cleared at the end of every loop, so all the aliases showed the same empty `list`. – ShadowRanger Mar 16 '21 at 18:15
  • @deceze Thanks for your reply. – Maghel Heans Gultom Mar 16 '21 at 18:54
  • @ShadowRanger and thanks for your explanation. Now I get it how it works – Maghel Heans Gultom Mar 16 '21 at 18:55

0 Answers0