[] works only if there is an element already at this index the list. Use list.append():
init = float(0.9)
l = []
for i in range(0,31):
l.append(init + ( float(i) / 100 ))
Once you are confortable with this, you can even use a comprehension list :
l = [init + (float(i) / 100 )) for i in range(0, 31)]
It is very rare in Python to use indexes. I understand a lot people do it because it's an habit in other languages, but most of the time, it's an anti-pattern in Python. If you you see a i somewhere, always wonder if you are not trying to reinvent the wheel.
BTW. Division has priority on addition so no need for parenthesis. Plus, init = float(0.9) is redundant. you can write init = 0.9. And the / always returns a float, therefor you can do :
l = []
for i in range(0, 31):
l.append(0.9 + i / 100)
Also note the way I place spaces. It's the most used style convention in Python.
And with a comprehension list :
l = [0.9 + i / 100 for i in range(0, 31)]
It is a much simpler way to achieve what you want. Don't worry, if your code works and you understand it, it's the most important. You don't NEED to do this. I'm just giving you this information so you can use it later if you wish.
Answer from Bite code on Stack Overflow[] works only if there is an element already at this index the list. Use list.append():
init = float(0.9)
l = []
for i in range(0,31):
l.append(init + ( float(i) / 100 ))
Once you are confortable with this, you can even use a comprehension list :
l = [init + (float(i) / 100 )) for i in range(0, 31)]
It is very rare in Python to use indexes. I understand a lot people do it because it's an habit in other languages, but most of the time, it's an anti-pattern in Python. If you you see a i somewhere, always wonder if you are not trying to reinvent the wheel.
BTW. Division has priority on addition so no need for parenthesis. Plus, init = float(0.9) is redundant. you can write init = 0.9. And the / always returns a float, therefor you can do :
l = []
for i in range(0, 31):
l.append(0.9 + i / 100)
Also note the way I place spaces. It's the most used style convention in Python.
And with a comprehension list :
l = [0.9 + i / 100 for i in range(0, 31)]
It is a much simpler way to achieve what you want. Don't worry, if your code works and you understand it, it's the most important. You don't NEED to do this. I'm just giving you this information so you can use it later if you wish.
Use the itertools module:
In [8]: for i in itertools.count(0.9, 0.01):
...: print i
...: if i > 1.2:
...: break
...:
0.9
0.91
0.92
0.93
.
.
.
1.18
1.19
1.2
As pointed out in the comments, the count() can be combined with a takewhile in order to create a list-comprehension:
[i for i in itertools.takewhile(lambda x: x <= 1.2, itertools.count(0.9, 0.01))]
filling an array with a loop in python - Stack Overflow
filling an array with a loop in python - Stack Overflow
python - fill an array line by line in a loop - Stack Overflow
fill empty array using loop in python - Stack Overflow
You don't really need to fill the array. np.linspace generates a uniformly spaced numpy array for you. However, given your requirement you can do this:
Solution 1: Loop method
import numpy as np
from numpy import log
#filling zeros
x_array = np.zeros(101)
y_array = np.zeros(101)
#generating uniformly spaced numbers
x=np.linspace(1,10,101)
#filling the array with loop
for i in range(0,len(x)):
x_array[i] = x[i] #filling x_array
y_array[i] = log(x[i]) #filling y_array
print(x_array,y_array)
Solution 2: List comprehension method
import numpy as np
from numpy import log
#filling zeros
x_array = np.zeros(101)
y_array = np.zeros(101)
#generating uniformly spaced numbers
x=np.linspace(1,10,101)
#filling the array with list comprehension
x_array = [ x[i] for i in range(0,len(x))]
y_array = [ log(x[i]) for i in range(0,len(x))]
print(len(x_array),len(y_array))
print(x_array,y_array)
Output:
#length
101 101
#x_array
[1.0, 1.09, 1.18, 1.27, 1.3599999999999999, 1.45, 1.54, 1.63, 1.72, 1.81, 1.9, 1.99, 2.08, 2.17, 2.26, 2.3499999999999996, 2.44, 2.5300000000000002, 2.62, 2.71, 2.8, 2.8899999999999997, 2.98, 3.07, 3.16, 3.25, 3.34, 3.4299999999999997, 3.52, 3.61, 3.6999999999999997, 3.79, 3.88, 3.9699999999999998, 4.0600000000000005, 4.15, 4.24, 4.33, 4.42, 4.51, 4.6, 4.6899999999999995, 4.779999999999999, 4.869999999999999, 4.96, 5.05, 5.14, 5.2299999999999995, 5.32, 5.41, 5.5, 5.59, 5.68, 5.77, 5.859999999999999, 5.95, 6.04, 6.13, 6.22, 6.31, 6.3999999999999995, 6.49, 6.58, 6.67, 6.76, 6.85, 6.9399999999999995, 7.029999999999999, 7.12, 7.21, 7.3, 7.39, 7.4799999999999995, 7.569999999999999, 7.66, 7.75, 7.84, 7.93, 8.02, 8.11, 8.2, 8.29, 8.379999999999999, 8.469999999999999, 8.559999999999999, 8.649999999999999, 8.739999999999998, 8.83, 8.92, 9.01, 9.1, 9.19, 9.28, 9.37, 9.459999999999999, 9.549999999999999, 9.64, 9.73, 9.82, 9.91, 10.0]
#y_erray
[0.0, 0.08617769624105241, 0.16551443847757333, 0.23901690047049992, 0.30748469974796055, 0.371563556432483, 0.43178241642553783, 0.4885800148186709, 0.5423242908253617, 0.5933268452777344, 0.6418538861723947, 0.688134638736401, 0.7323678937132266, 0.7747271675523681, 0.8153648132841944, 0.8544153281560675, 0.8919980393051105, 0.928219302739429, 0.9631743177730056, 0.9969486348916096, 1.0296194171811581, 1.0612565021243408, 1.091923300517313, 1.1216775615991057, 1.1505720275988207, 1.1786549963416462, 1.205970806988609, 1.2325602611778486, 1.2584609896100056, 1.2837077723447896, 1.3083328196501787, 1.332366019094335, 1.355835153635182, 1.378766094699099, 1.4011829736136414, 1.423108334242607, 1.4445632692438664, 1.4655675420143985, 1.4861396960896067, 1.506297153514587, 1.5260563034950492, 1.5454325824581878, 1.5644405465033644, 1.5830939370944983, 1.601405740736836, 1.6193882432872684, 1.6370530794670737, 1.6544112780768314, 1.6714733033535532, 1.6882490928583902, 1.7047480922384253, 1.7209792871670078, 1.7369512327330598, 1.7526720805200082, 1.7681496035889213, 1.7833912195575383, 1.7984040119467235, 1.81319474994812, 1.827769906751088, 1.8421356765531218, 1.856297990365626, 1.8702625307159986, 1.884034745337226, 1.8976198599275322, 1.9110228900548727, 1.9242486522741338, 1.937301774518713, 1.9501867058225735, 1.9629077254238845, 1.9754689512968577, 1.9878743481543455, 2.0001277349601105, 2.012232791986386, 2.024193067449357, 2.0360119837525, 2.0476928433652555, 2.0592388343623163, 2.0706530356467567, 2.081938421878423, 2.0930978681273213, 2.1041341542702074, 2.1150499691472033, 2.1258479144939915, 2.136530508663963, 2.1471001901536506, 2.157559320943788, 2.167910189667444, 2.178155014615869, 2.188295946591918, 2.1983350716202463, 2.2082744135228043, 2.2181159363675955, 2.2278615467981093, 2.2375130962503307, 2.2470723830637866, 2.2565411544926386, 2.265921108622454, 2.2752138961979136, 2.2844211223663744, 2.2935443483418965, 2.302585092994046]
EDIT:
Adding the plotting functionality:
#add this code below the above code if you want a plot
import matplotlib.pyplot as plt
plt.plot(x_array,y_array)
plt.title(r'
')
plt.show()
Output:

You can generate x_array using range() and then fill y_array accordingly using math.log():
from math import log
x_array = list(range(1, 11))
y_array = []
for val in x_array:
y_array.append(log(val))
print(x_array)
print(y_array)
Or, a bit concisely, use list-comprehension:
from math import log
x_array = list(range(1, 11))
y_array = [log(val) for val in x_array]
print(x_array)
print(y_array)
You can easily do this by using list comprehension.
xrange(0, len(crave), 3) is used to iterate from 0 to len(crave) with an interval of 3. and then we use list slicing crave[i:i+3] to extract the required data.
crave=['word1','word2','word3','word4','word5','word6','word7']
ITEM = [crave[i:i+3] for i in xrange(0, len(crave), 3)]
print ITEM
>>> [['word1', 'word2', 'word3'], ['word4', 'word5', 'word6'], ['word7']]
try to this.
crave = ['word1', 'word2', 'word3', 'word4', 'word5', 'word6', 'word7']
li = []
for x in range(0, len(crave), 3):
li.append(crave[x:x+3])
print li
You can easily do this by using list comprehension.
xrange(0, len(crave), 3) is used to iterate from 0 to len(crave) with an interval of 3. and then we use list slicing crave[i:i+3] to extract the required data.
crave=['word1','word2','word3','word4','word5','word6','word7']
ITEM = [crave[i:i+3] for i in xrange(0, len(crave), 3)]
print ITEM
>>> [['word1', 'word2', 'word3'], ['word4', 'word5', 'word6'], ['word7']]
try to this.
crave = ['word1', 'word2', 'word3', 'word4', 'word5', 'word6', 'word7']
li = []
for x in range(0, len(crave), 3):
li.append(crave[x:x+3])
print li
Your code at the moment seems very hard coded I'm unsure as to what you are trying to achieve but try:
DU = []
for index, line in enumerate(oz):
line = line.strip() if (index +1) % 15 != 0 else line.strip().split(' ')[0]
for i in range(0,len(line)-3,3):
DU.append(line[i:i+3])
or could you try a combination of the answers
from itertools import izip
def grouped(iterable, n):
"s -> (s0,s1,s2,...sn-1), (sn,sn+1,sn+2,...s2n-1), (s2n,s2n+1,s2n+2,...s3n-1), ..."
return izip(*[iter(iterable)]*n)
DU = []
for index, line in enumerate(oz):
line = line.strip() if (index +1) % 15 != 0 else line.strip().split(' ')[0]
DU.append(map(''.join, grouped(line.strip(), 3)))
You might be able to use something along the lines of the update in my answer to another question here to group the string of digits in each number. Specifically using this code with the number string as the iterable and a value of 3 for the n (group-size) argument:
from itertools import izip
def grouped(iterable, n):
"s -> (s0,s1,s2,...sn-1), (sn,sn+1,sn+2,...s2n-1), (s2n,s2n+1,s2n+2,...s3n-1), ..."
return izip(*[iter(iterable)]*n)
digits = '283283283283283283283283284284284284284284284284284284284284284284284284284\n'
print map(''.join, grouped(digits.strip(), 3))
Output:
['283', '283', '283', '283', '283', '283', '283', '283', '284', '284',
'284', '284', '284', '284', '284', '284', '284', '284', '284', '284',
'284', '284', '284', '284', '284']
I note however that the last line of the data in your example, the:
'294294294294294294294294295295 lat = -89.5\n'
is not simply a string of digits, so it will have to be handled as a special case.
Update:
OK, now that I see the additional information you added to your question, I can provide a complete solution for you based on the grouped() function from my other answer, as I initially suggested. This handles the special line which occurs periodically in your input data by splitting up each line of data and then ignoring all but the first -- often the only -- item of that, which is always a string of digits which are then further processed via my function.
from itertools import izip
def grouped(iterable, n):
"s -> (s0,s1,s2,...sn-1), (sn,sn+1,sn+2,...s2n-1), (s2n,s2n+1,s2n+2,...s3n-1), ..."
return izip(*[iter(iterable)]*n)
data = [' 283283283283283283283283284284284284284284284284284284284284284284284284284\n', ' 284284284284284284284284284284284284284284284284284284284284284284284284284\n', ' 284284284284284284284284284284284284284284284284284284284284284284284284284\n', ' 284284284284284284284284284284284284284284284284284284284284284284284284284\n', ' 284284284284284284284284284284284284284284284284284284284284284284283283283\n', ' 283283283283283283283283283283283283283283283283283283283283283283283283283\n', ' 283283283283283283283283283283283283283283283283283283283283283283283283283\n', ' 283283283283283283283283283283283283283283283283283283283283283283283283283\n', ' 283283283283283282282283283282282282282283283283283283283283283283283283283\n', ' 283283283283283283283283283283283283283283283283283283283283283284284284284\n', ' 284284284284284284284284284284284285285285285285285285285285285285285285285\n', ' 285285286286286286286286286287287287287287287288288288288288288288288288288\n', ' 288289289289289289289289289290290290290290290290290290291291291291291291291\n', ' 291291292292292292292292293293293293293293293293293293293293294294294294294\n', ' 294294294294294294294294295295 lat = -89.5\n', ' 284284284284284284284284284284284284284284284284284284284284284284284284284\n', ' 284284284284284284284284284284283283284284284284284284284284284284284284283\n', ' 283283283283283283283283283283283283283283283283283283283283283283283283283\n', ' 283283283283283283283283283283284284284284284284284284284284284284284284284\n', ' 284284284284284284284284284284284284283283283283283283283283283283283283283\n', ' 283283283283283283283283283283283282282282282282282282282282282282282282281\n', ' 281281281281281281281281281281281281281281281281280280280280280280280280279\n', ' 279279279279279279279279279279279279279279278278278278278278278278278278278\n', ' 277277278278278278278278278278278278278278278278278278278278278278278278278\n', ' 278278279279279279279279279279279279279279279279279279279279279279279279279\n', ' 279279280280280280280280280280280280280280280280280280280281281281281281281\n', ' 281282282282282282282282283283283283283283284284284284284284285285285285285\n', ' 286286286287287287287288288288288288288289289289289289290290290290291291291\n', ' 292292292292292292293293293293293293293293293294294294294295295295295295295\n', ' 296296296296296296296297297297 lat = -88.5\n']
DU = []
for line in data:
DU.extend(map(''.join, grouped(line.strip().split()[0], 3)))
print DU
Output:
['283', '283', '283', '283', '283', '283', '283', '283', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '282', '282', '283', '283', '282', '282', '282', '282', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '285', '285', '285', '285', '285', '285', '285', '285', '285', '285', '285', '285', '285', '285', '285', '285', '286', '286', '286', '286', '286', '286', '286', '287', '287', '287', '287', '287', '287', '288', '288', '288', '288', '288', '288', '288', '288', '288', '288', '288', '289', '289', '289', '289', '289', '289', '289', '289', '290', '290', '290', '290', '290', '290', '290', '290', '290', '291', '291', '291', '291', '291', '291', '291', '291', '291', '292', '292', '292', '292', '292', '292', '293', '293', '293', '293', '293', '293', '293', '293', '293', '293', '293', '293', '294', '294', '294', '294', '294', '294', '294', '294', '294', '294', '294', '294', '294', '295', '295', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '283', '283', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '284', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '283', '282', '282', '282', '282', '282', '282', '282', '282', '282', '282', '282', '282', '282', '281', '281', '281', '281', '281', '281', '281', '281', '281', '281', '281', '281', '281', '281', '281', '281', '281', '280', '280', '280', '280', '280', '280', '280', '280', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '277', '277', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '278', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '279', '280', '280', '280', '280', '280', '280', '280', '280', '280', '280', '280', '280', '280', '280', '280', '280', '280', '281', '281', '281', '281', '281', '281', '281', '282', '282', '282', '282', '282', '282', '282', '283', '283', '283', '283', '283', '283', '284', '284', '284', '284', '284', '284', '285', '285', '285', '285', '285', '286', '286', '286', '287', '287', '287', '287', '288', '288', '288', '288', '288', '288', '289', '289', '289', '289', '289', '290', '290', '290', '290', '291', '291', '291', '292', '292', '292', '292', '292', '292', '293', '293', '293', '293', '293', '293', '293', '293', '293', '294', '294', '294', '294', '295', '295', '295', '295', '295', '295', '296', '296', '296', '296', '296', '296', '296', '297', '297', '297']
You could make an efficient fairly unreadable list comprehension out of it like this:
from itertools import chain
DU = list(chain.from_iterable(map(''.join, grouped(line.strip().split()[0], 3))
for line in data))
I like list comprehension!
print [[x, y] for x in range(3) for y in range(3)]
But it creates three lists (the iterated two stay in memory) in python 2.7 which is the common one nowdays.
If you're using big values try this:
print [[x, y] for x in xrange(3) for y in xrange(3)]
which uses Generators :)
You can append the lists to the result:
result = []
for i in range(3):
for j in range(3):
paths = [i,j]
result.append(paths)
print paths
print result
This says: create a result list [] and append the paths value to it at each step. You'll then have a list of lists and you can access the first element as result[0] (which is indeed [0, 0]).
Appending elements while looping using append() is correct and it's a built-in method within Python lists.
However you can have the same result:
Using list comprehension:
result_t = [k for k in range(1,6)]
print(result_t)
>>> [1, 2, 3, 4, 5]
Using + operator:
result_t = []
for k in range(1,6):
result_t += [k]
print(result_t)
>>> [1, 2, 3, 4, 5]
Using special method __iadd__:
result_t = []
for k in range(1,6):
result_t.__iadd__([k])
print(result_t)
>>> [1, 2, 3, 4, 5]
The range function returns an iterator in modern Python. The list function converts an iterator to a list. So the following will fill your list with the values 1 to 5:
result_t = list(range(1,6)) # yields [1, 2, 3, 4, 5]
Note that in order to include 5 in the list, the range argument has to be 6.
Your last example doesn't parse unless you assign t a value before the loop. Assuming you do that, what you're doing in that case is modifying t each time through the loop, not just producing a linear range. You can get this effect using the map function:
t = 0
b = 2
def f(i):
global t
t = i + b*t
return t
result_b = list(map(f, range(1, 5))) # Yields [1, 4, 11, 26]
The map function applies the f function to each element of the range and returns an iterator, which is converted into a list using the list function. Of course, this version is more verbose than the loop, for this small example, but the technique itself is useful.
Using np.repeat and np.tile on x and y respectively to get the desired columns and then stacking them together with z into one array:
Use with AIimport numpy as np
x = [1, 2, 3]
y = [4, 5]
z = [6, 7, 8, 9, 10, 11]
a = np.array([np.repeat(x, 2), np.tile(y, 3), z]).T
result:
Use with AIarray([[ 1, 4, 6],
[ 1, 5, 7],
[ 2, 4, 8],
[ 2, 5, 9],
[ 3, 4, 10],
[ 3, 5, 11]])
For a pure Python solution, to work with any list size.
Use with AIx = [11,12,13,14,15]
y = [21,23,24]
z = [31,32,33,34,35,36,37]
This verbose code:
Use with AItmp = []
for i in range(max(len(x), len(y), len(z))):
for yy in y:
tmp.append([x[i%len(x)], yy])
for i in range(len(tmp)):
tmp[i].append((z[i%len(z)]))
Returns
Use with AI# [[11, 21, 31],
# [11, 23, 32],
# [11, 24, 33],
# [12, 21, 34],
# [12, 23, 35],
# [12, 24, 36],
# [13, 21, 37],
# [13, 23, 31],
# [13, 24, 32],
# [14, 21, 33],
# [14, 23, 34],
# [14, 24, 35],
# [15, 21, 36],
# [15, 23, 37],
# [15, 24, 31],
# [11, 21, 32],
# [11, 23, 33],
# [11, 24, 34],
# [12, 21, 35],
# [12, 23, 36],
# [12, 24, 37]]
Sorry struggling with the code that's in the sample. From your description I can see that you want to generate Pascal's triangle. Here's a short snippet that will do this.
a = [1, 1]
for _ in range(10):
a = [1] + [x+y for (x,y) in zip(a[:-1], a[1:])] + [1]
print a
a[:-1] refers to the whole array except the last element and a[1:] refers to whole array except first element. zip combines first elements from each array into a tuple and so on. All that remains is to add them and pad the row with ones one the outside. _ is used to tell Python, I don't care about this variable - useful if you want to be explicit that you are not using the range value for anything except flow control.
Maria's answer is perfect, I think. If you want to start with your code, you can rewrite your code as below to get similar result. FYI.
n = 3
b = [1, 2, 1]
while 1 < n < 10:
if n % 2 == 0:
print("even")
b = [0] * (n + 1)
for i in range(0, n + 1):
if i == 0:
b[i] = a[0]
elif i == n:
b[i] = a[i - 1]
else:
b[n - i] = a[i - 1] + a[i]
else:
print("odd")
a = [0] * (n + 1)
for i in range(0, n + 1):
if i == 0:
a[i] = b[0]
elif i == n:
a[i] = b[i - 1]
else:
a[i] = b[i - 1] + b[i]
if n % 2 == 0:
print(b)
else:
print(a)
n += 1
print("loop")
Hello everyone,
I haven't done any type of linear algebra work with python before, only matlab. In matlab you create a matrix with the proper dimensions, then start filling it up with vectors of the appropriate size. I've tried to look up how to do this in python using a loop, and it doesn't seem as straightforward. It seems you first need to create a list, and append that list. Then convert that list to a numpy array. And then from there it appears you stack them? But stacking seems to only stack 1D and 2D arrays, so it's not exactly what I'm looking for.
For example:
import numpy as np
matrix=np.zeros((rows,columns))
list=[]
for i in range(columns):
for x in range(rows):
list.append(x)
arr=np.array(list)
matrix.append(arr)
list.clear() So in the above case, I've made a list, and converted it into an array with shape (rows, 1). Then I want to add it to the first column of my matrix. Then through the loop, I start to fill my matrix up by filling it one by one with these vectors. However, the issue is I'm trying to add a 1D array, to a multi-D matrix (which numpy doesn't allow you to do).
Hpw do I fill up a matrix using numpy?
First you have to install numpy using
$ pip install numpy
Then the following should work
import numpy as np
n = 100
matrix = np.zeros((n,2)) # Pre-allocate matrix
for i in range(1,n):
matrix[i,:] = [3*i, i**2]
A faster alternative:
col1 = np.arange(3,3*n,3)
col2 = np.arange(1,n)
matrix = np.hstack((col1.reshape(n-1,1), col2.reshape(n-1,1)))
Even faster, as Divakar suggested
I = np.arange(n)
matrix = np.column_stack((3*I, I**2))
This is very pythonic form to produce a list, which you can easily swap e.g. for np.array, set, generator etc.
n = 10
[[i*3, i**2] for i, i in zip(range(0,n), range(0,n))]
If you want to add another column it's no problem. Simply
[[i*3, i**2, i**(0.5)] for i, i in zip(range(0,n), range(0,n))]