How to create an array of empty lists?
Question:
I’m struggling a bit with NumPy’s build in arrays.
I have a list with values:
l = ["a", "b", "c", "d", "e"]
The len(l)
of the list is 5 and I want to create an array with the following structure.
array = [[][][][][]]
The inner array amount should be the length of the list l
. (5)
Is there any solution to this with NumPy or is there a solution doing this with the build in list from python.
Answers:
Using python:
array = [[] for _ in range(len(l))]
Output: [[], [], [], [], []]
With numpy:
array = np.zeros(shape=(len(l), 0))
Output: array([], shape=(5, 0), dtype=float64)
(this is the same as np.array([[], [], [], [], []])
)
With bulit in list:
n = 5
arr = [[] for x in range(n)]
arr
I’m struggling a bit with NumPy’s build in arrays.
I have a list with values:
l = ["a", "b", "c", "d", "e"]
The len(l)
of the list is 5 and I want to create an array with the following structure.
array = [[][][][][]]
The inner array amount should be the length of the list l
. (5)
Is there any solution to this with NumPy or is there a solution doing this with the build in list from python.
Using python:
array = [[] for _ in range(len(l))]
Output: [[], [], [], [], []]
With numpy:
array = np.zeros(shape=(len(l), 0))
Output: array([], shape=(5, 0), dtype=float64)
(this is the same as np.array([[], [], [], [], []])
)
With bulit in list:
n = 5
arr = [[] for x in range(n)]
arr