I did not know that...
JavaScript
const data_list = [];
let data = [];
data[0] = 0;
data[1] = 1;
data[2] = 2;
data_list.push(data);
console.log('data:', data); // [0, 1, 2]
console.log('data_list:', data_list); // [[0, 1, 2]]
// data = [];
data[0] = 3;
data[1] = 4;
data[2] = 5;
data_list.push(data);
console.log('data:', data); // [3, 4, 5]
console.log('data_list:', data_list); // [[3, 4, 5], [3, 4, 5]]
Python
data_list = []
data = [0, 0, 0]
data[0] = 0
data[1] = 1
data[2] = 2
data_list.append(data)
print ('data: ', data) # [0, 1, 2]
print ('data_list: ', data_list) # [[0, 1, 2]]
# data = [0, 0, 0]
data[0] = 3
data[1] = 4
data[2] = 5
data_list.append(data)
print ('data: ', data) # [3, 4, 5]
print ('data_list: ', data_list) # [[3, 4, 5], [3, 4, 5]]
If you think about it, it seems obvious, but since the addresses of the array added the first time and the array added the second time are the same, they are the same value by the time they are referenced.
To get the expected result [[0, 1, 2], [3, 4, 5]]
, you need to initialize the array at the right time.
Do not overwrite the contents of the array by stopping thinking. Even if you are using a scripting language, be aware of memory when dealing with arrays.
By the way, I buried the data that was supposed to be collected in the experiment by this method in the darkness (crying) (stupid)
Recommended Posts