Let's say I have an array that is 10 x 100,000. What is the simplest and/or fastest way to initialize this? For example, something like:
[None,] * cols # now how to do it by # rows?
Let's say I have an array that is 10 x 100,000. What is the simplest and/or fastest way to initialize this? For example, something like:
[None,] * cols # now how to do it by # rows?
If you want to do this with vanilla Python lists, I would use a list comprehension:
big_array = [[None]*100000 for j in range(10)]
However, if you are going to be working with large arrays a lot, I would consider using using numpy:
import numpy as np
another_big_array = np.empty((10, 100000))
With numpy, be sure you get all 4 parentheses. np.empty() takes a single argument for the shape of the array, so for multidimensional arrays you need a tuple of integers, not multiple arguments.
Hope that helps!
[None]*100000 for a speed boost. (The outer comprehension cannot be replaced this way, or you'll have aliasing problems.)Try using the numpy.full method.
Example of a 2x2 array
import numpy as np
none_array = np.full((2, 2), None)
print(none_array)