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