I want to convert variables created by range into matrices.

Asked 2 years ago, Updated 2 years ago, 37 views

for i in range (5)

Output from the

0
1
2
3
4

I would like to convert the result into a single-line matrix as follows.

 [0,1,2,3,4]

Thank you for your cooperation.

python python3

2022-09-30 21:28

1 Answers

If you are using range to create a list with consecutive numbers, you can use the following methods:

list(range(5))

If the purpose is not a list but a numpy matrix,

import numpy
numpy.array(range(5))

and so on.

Additional information: November 26, 2017

 A = [ ]
idx = 0
while idx <4:
    A.append(idx+1)
    idx+=1
print(A)


2022-09-30 21:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.