I am doing this AI project in which I need to create an array of any 5 consecutive numbers. For example [[[1],[2],[3],[4],[5]]]. Luckily, I am following a tutorial and got this line to do it: Data = [[[i+j] for i in range(5)] for j in range(100)]
. I want to know what this means. I have strong knowledge of Python but have never used this type of notation.
The code
Data = [[[i+j] for i in range(5)] for j in range(100)]
Can be cut down to two pieces:
[[i+j] for i in range(5)]
and
[[[i+j] for i in range(5)] for j in range(100)]
Both of them contain a list comprehension. Let's evaluate the first one.
[[i+j] for i in range(5)]
This is similar to:
elements = []
for i in range(5):
elements.append([i + j])
Which produces:
[[0], [1], [2], [3], [4]]
The outer loop performs this task a hundred times, but increases the inner loop start value by 1 on each run. So we generate 100 lists, containing a list of 5 elements, each containing a list of 1 element.