Why not try something like this to transpose the columns:
x = []
for d in xrange(0,66):
x.append(data[:,d])
Unless it's absolutely essential that there is a separate data structure for each item, although I don't know why you would need separate data strucures...
EDIT: If not here's something that should work precisely the way you described:
for d in xrange(1,68):
exec 'x%s = data[:,%s]' %(d,d-1)
Answer from Master_Yoda on Stack OverflowWhy not try something like this to transpose the columns:
x = []
for d in xrange(0,66):
x.append(data[:,d])
Unless it's absolutely essential that there is a separate data structure for each item, although I don't know why you would need separate data strucures...
EDIT: If not here's something that should work precisely the way you described:
for d in xrange(1,68):
exec 'x%s = data[:,%s]' %(d,d-1)
As you show a little bit of the rpy code, I thought that I could show how it would look like with rpy2.
# build a DataFrame
from rpy2.robjects.vectors import IntVector
d = dict(('x%i' % (i+1), IntVector(data[:, i]) for i in range(68) if i != 9)
d['y'] = data[:, 9]
from rpy2.robjects.vectors import DataFrame
dataf = DataFrame(d)
del(d) # dictionary no longer needed
# import R's stats package
from rpy2.robjects.packages import importr
stats = importr('stats')
# fit model
dep_var = 'y'
formula = '%s ~ %s ' % (dep_var, '+'.join(x for x in dataf.names if x != dep_var))
linear_model = stats.lm(formula, data = dataf)
In the first one you add the arrays of length 10 to the bigger array. So you need to create two arrays.
array1 = []
array2 = []
for j in range(20):
for i in range(10):
array1.append(0)
array2.append(array1)
array1 = []
print array2
This is equivalent to
array2=[[0 for j in range(10)] for i in range(20)]
You need to create a temporary list inside outer for/while loop which you can fill inside inner for/while loop.
First:
>>> for j in range(20):
... temp=[]
... for i in range(10):
... temp.append(0)
... array1.append(temp)
...
>>> array1
Second:
>>> count=0
>>> array3=[]
>>> while count < 20:
... temp=[]
... count_inner=0
... count+=1
... while count_inner< 10:
... temp.append(0)
... count_inner+=1
... array3.append(temp)
>>> array3
With your conditions in while loop check you were creating 21 X 11 matrix.
Videos
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.
Hi looking for help with a current project Iโm doing. I have a variable Number_Of_items which is an integer number read from a .txt file. Im trying to create a number of arrays equivalent to the variable Number_Of_items . For example if the value for Number_Of_items was 6, I would want to create 6 arrays with names array1, array2 , array3 etc. if anyone could point me in the right direction it would be appreciated.
You could preallocate the array before assigning the respective values:
a = np.empty(shape=(25, 2), dtype=int)
for x in range(1, 6):
for y in range(1, 6):
index = (x-1)*5+(y-1)
a[index] = x, y
Did you had a look at numpy.ndindex? This could do the trick:
a = np.ndindex(6,6)
You could have some more information on Is there a Python equivalent of range(n) for multidimensional ranges?
You get a metar_dat array that is mostly 0 because it is the one you created at the last k iteration. It was len(stat_id) long (in the 1st dimensions) but you only inserted data for the last k. You threw away the results for the earlier k.
I would suggest collecting the data in a dictionary, rather than object array.
metar_dat = dict() # dictionary rather than object array
for id in stat_id:
# Bring all the data into one big array.
data = np.column_stack([yr, month, day, time,temp, dwp])
# should produce as (len(temp),6) integer array
# or float is one or mo for k in range(len(stat_id)):
metar_dat[id] = data
If len(temp) varies for each id, you can't make a meaningful 3d array with shape (len(stat_id), len(temp), 7) - unless you pad every one to the same maximum length. When thinking about arrays, thing rectangles, not ragged lists.
A Python dictionary is a much better way of collecting information by some sort of unique id.
Object arrays let you generalize the concept of numeric arrays, but they don't give much added power compared to lists or dictionaries. You can't for example, add values across the 'id' dimension.
You need to describe what you hope to do with this data once you collect it. That will help guide our recommendations regarding the data representation.
There are other ways of defining the data structure for each id. It looked like yr, time, temp were equal length arrays. If they are all numbers they could be collected into an array with 6 columns. If it is important to keep some integer, while others are floats (or even strings) you could use a structured array.
Structured arrays are often produced by reading column data from a csv file. Some columns will have string data (ids) others integers or even dates, others float data. np.genfromtxt is a good tool for loading that sort of file.
You're setting your 2D array to zero inside your k-loop each time. Set it to zero (or empty, if all elements get filled, as in your case) once outside your nested loop, and you should be fine:
metar_dat = np.empty((len(stat_id),len(temp),7), dtype='object')
for k in range(len(stat_id)):
for i in range(len(temp)):
metar_dat[k,i] = np.dstack((stat_id[k], yr[i], month[i], day[i], time[i], temp[i], dwp[i]))
return metar_dat