Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a 0 to n vector in python

I am a new python user and I was wondering how I could make a 0 to n vector. I would like the user to be able to input an integer for n, and receive an output of [0,1,2,3,4,5...,n].

This is what I have done so far...

from numpy import matrix

n=int(raw_input("n= "))
for i in range(n, 0, -1):
K = matrix(i)
print K

But this is what I get as an output:

[0][1][2][3][4][5]...[n]

Transposing the matrix doesn't help. What am I doing wrong?

Thank you for your help!

like image 993
Xander Avatar asked Jul 23 '12 03:07

Xander


3 Answers

If you want to use numpy, you can make use of arange:

>>> import numpy as np
>>> np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
like image 83
Akavall Avatar answered Nov 27 '22 13:11

Akavall


Use the built-in function:

range(n)

(Well, should be n+1 if you want a list to be [0, 1, ... , n])

like image 20
joe Avatar answered Nov 27 '22 13:11

joe


from numpy import array
n = int(raw_input("n= "))
k = array(range(n+1))
print k
like image 27
John La Rooy Avatar answered Nov 27 '22 14:11

John La Rooy