Reputation: 501
Good morning,
Given two numbers (a,b), I should create 'a' different list of length 'b' where the sequence of each list is increased by one.
For example :
1,2 =>[[0,1]]
2,2 = > [[0,1], [1,2]]
I am trying to write the following function:
def increase_arrays(arrays, length):
result = [[i for i in range(length)] for i in range(arrays)]
return result
increase_arrays(2,3)
=> [[0, 1, 2], [0, 1, 2]]
I can't see how to modify my code so the second array is [1,2,3]
.
Could anyone help to resolve the issue?
Upvotes: 0
Views: 60
Reputation: 501
I finally managed to solve it with:
def increase_arrays(arrays, length):
return [list(range(i,length+i)) for i in range(arrays)]
Upvotes: 1
Reputation: 45752
Here's a numpy solution, just for fun:
np.arange(arrays)[:,np.newaxis] + np.arange(length)[np.newaxis,:]
Upvotes: 0
Reputation: 3651
You have famous duplicated index problem, you have 2 indexes i
.
Below is the code that you need:
def increase_arrays(arrays, length):
result = [[i + j for i in range(length)] for j in range(arrays)]
return result
increase_arrays(2, 3)
returns [[0, 1, 2], [1, 2, 3]]
Upvotes: 4