I have a loop which generates a value_list each time it runs, at the end of each iteration i want to append all the lists into a one multi dimensional array
I have:
value_list = [1,2,3,4]in 1st iterationvalue_list = [5,6,7,8]in 2nd iterationvalue list = [9,10,11,12]in 3rd iteration- etc...
At the end of each iteration I want one multi dimensional array like
value_list_copy = [[1,2,3,4]]in the 1st iterationvalue_list_copy = [[1,2,3,4],[5,6,7,8]]in the 2nd iterationvalue_list_copy = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]- etc...
How could I achieve this?
Thanks
-
I rolled back your edit because after that edit the question altogether became a new question. If you have to respect the time the users put in to answer the question you posted. All of a sudden you can't just edit and ask a new question. You can always ask another question. To ask new question click hereCh3steR– Ch3steR2020年04月09日 09:45:06 +00:00Commented Apr 9, 2020 at 9:45
-
sorry...i am new to this... will post a different questionPriya Ramakrishnan– Priya Ramakrishnan2020年04月09日 09:52:44 +00:00Commented Apr 9, 2020 at 9:52
-
Don't be... BTW Welcome to Stack Overflow. I just wanted to tell how things work here. ;)Ch3steR– Ch3steR2020年04月09日 09:55:14 +00:00Commented Apr 9, 2020 at 9:55
-
@PriyaRamakrishnan You can mark any of the answers as your solution to thank the people taking time answering your question (and it will also gives you some additional reputation). Cheers and Welcome to the best community in the world~!Jean-Francois T.– Jean-Francois T.2020年04月10日 04:10:31 +00:00Commented Apr 10, 2020 at 4:10
6 Answers 6
You can use a nested comprehension and itertools.count:
from itertools import count, islice
cols = 4
rows = 5
c = count(1)
matrix = [[next(c) for _ in range(cols)] for _ in range(rows)]
# [[1, 2, 3, 4],
# [5, 6, 7, 8],
# [9, 10, 11, 12],
# [13, 14, 15, 16],
# [17, 18, 19, 20]]
The cool kids might also want to zip the count iterator with itself:
list(islice(zip(*[c]*cols), rows))
# [(1, 2, 3, 4),
# (5, 6, 7, 8),
# (9, 10, 11, 12),
# (13, 14, 15, 16),
# (17, 18, 19, 20)]
Comments
If you are using Python3.8 then use Walrus assignment(:=).
For Syntax and semantic.
count=0
rows=5
cols=4
[[(count:=count+1) for _ in range(cols)] for _ in range(rows)]
Output:
[[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
[17, 18, 19, 20]]
Without using :=.
rows=5
cols=4
[list(range(i,i+cols)) for i in range(1,rows*cols,cols)]
1 Comment
Try this:
limit = 10
length_of_elements_in_each_list = 4
[range(i, i+length_of_elements_in_each_list) for i in range(1, limit)]
You can set a limit and length_of_elements_in_each_list according to your need.
2 Comments
list of range objects not a list of list objects. Even if you do [list(range(i, i+length_of_elements_in_each_list)) for i in range(1, limit)] to make it a list of list objects, the nested lists don't match the pattern described in the question.print([[*range(i, i+length_of_elements_in_each_list)] for i in range(1, limit)])Try this below :
value_list_copy = []
for i in range(n): # ----------> Assuming n is the number of times your loop is running
value_list_copy.append(value_list) # ------ Append your value list in value_list_copy in every iteration
Here you will get an array of arrays.
print(value_list_copy)
Comments
Here are two other possible solutions:
Double for loop approach
rows, cols, start = 3, 4, 1
value_list_copy = []
for j in range(rows):
value_list = []
for i in range(start, cols + start):
value_list.append((j*cols)+i)
value_list_copy.append(value_list)
print(
f'value_list = {value_list}\n'
f'value_list_copy = {value_list_copy}\n'
)
List comp method
rows, cols, start = 3, 4, 1
value_list_copy_2 = [
[
(j*cols)+i for i in range(start, cols + start)
] for j in range(rows)
]
print(f'value_list_copy_2 = {value_list_copy_2}')
Comments
(lambda x,y: np.arange(1,1+x*y).reshape((x,y)).tolist())(3,4)
Comments
Explore related questions
See similar questions with these tags.