在 Python 中,是否可以模拟 JavaScript 数组(即,在数组范围之外添加值时自动扩展的数组)?在 JavaScript 中,当在数组的索引之外分配值时,数组会自动扩展,但在 Python 中,它们不会:
theArray = [None] * 5
theArray[0] = 0
print(theArray)
theArray[6] = 0 '''This line is invalid. Python arrays don't expand automatically, unlike JavaScript arrays.'''
这在 JavaScript 中是有效的,我试图在 Python 中模仿它:
var theArray = new Array();
theArray[0] = 0;
console.log(theArray);
theArray[6] = 0; //the array expands automatically in JavaScript, but not in Python