Thursday, 19 September 2013

Python iterate over two nested 2D lists where list2 has list1's row numbers

Python iterate over two nested 2D lists where list2 has list1's row numbers

I'm new to Python. So I want to get this done with loops without using
some fancy stuff like generators. I have two 2D arrays, one interger array
and the other string array like this:
I) Integer 2D list:
Here, dataset2d[0][0] is number of rows in the table, dataset[0][1] is
number of columns. So the below 2D list has 6 rows and 4 columns
dataset2d[][]:
6 4
0 0 0 1
1 0 2 0
2 2 0 1
1 1 1 0
0 0 1 1
1 0 2 1
II) String 2D list:
partition2d[][]
A 1 2 4
B 3 5
C 6
partition[*][0] i.e first column is a label. For group A, 1,2 and 4 are
the row numbers that I need to pick up from dataset2d and apply a formula.
So it means I will read 1, go to row 1 in dataset2d and read the first
column value i.e dataset2d[1][0], then I will read 2 from partition2d, go
to row 2 of dataset 2d and read the first column i.e dataset2d[2][0].
Similarly next one I'll read dataset2d[4][0].
Then I will do some calculations, get a value and store it in a 2D list,
then go to the next column in dataset2d for those rows. So in this
example, next column values read would be dataset2d[1][1],
dataset2d[2][1], dataset2d[4][1]. And again do some calculation and get
one value for that column, store it. I'll do this until I reach the last
column of dataset2d.
The next row in partition2d is B, 3, 5. So I'll start with
dataset2d[3][0], dataset2d[5][0]. Get a value for that column be a
formula. Then real dataset2d [3][1], dataset2d[5][1] stc. until I reach
last column. I do this until all rows in partition2d are read.
What I tried:
for partitionRow in partition2d:
for partitionCol in partitionRow:
for colDataset in dataset2d:
print dataset2d[partitionCol][colDataset]
What problem I'm facing:
partition2d is a string array where I need to skip the first column which
has characters like A,B,C.
I want to iterate in dataset2d column wise only over the row numbers given
in partition2d. So the colDataset should increment only after I'm done
with that column.

No comments:

Post a Comment