Note that there are some explanatory texts on larger screens.

plurals
  1. POIterate over two nested 2D lists where list2 has list1's row numbers
    primarykey
    data
    text
    <p>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 integer array and the other string array like this:</p> <ol> <li><p>Integer 2D list: </p> <p>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</p> <pre><code>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] ] </code></pre></li> <li><p>String 2D list: </p> <pre><code>partition2d = [ ['A', '1', '2', '4'], ['B', '3', '5'], ['C', '6'] ] </code></pre> <p><code>partition[*][0]</code> 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 <code>dataset2d</code> and read the first column value i.e <code>dataset2d[1][0]</code>, then I will read 2 from <code>partition2d</code>, go to row 2 of dataset 2d and read the first column i.e <code>dataset2d[2][0]</code>. Similarly next one I'll read <code>dataset2d[4][0]</code>.</p> <p>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 <code>dataset2d[1][1]</code>, <code>dataset2d[2][1]</code>, <code>dataset2d[4][1]</code>. 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 <code>dataset2d</code>. </p> <p>The next row in <code>partition2d</code> is <code>[B, 3, 5]</code>. So I'll start with <code>dataset2d[3][0]</code>, <code>dataset2d[5][0]</code>. Get a value for that column be a formula. Then real <code>dataset2d [3][1]</code>, <code>dataset2d[5][1]</code> etc. until I reach last column. I do this until all rows in partition2d are read.</p></li> </ol> <p>What I tried:</p> <pre><code> for partitionRow in partition2d: for partitionCol in partitionRow: for colDataset in dataset2d: print dataset2d[partitionCol][colDataset] </code></pre> <p>What problem I'm facing:</p> <ol> <li>partition2d is a string array where I need to skip the first column which has characters like A,B,C. </li> <li>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.</li> </ol> <p>Update1:</p> <p>I'm reading the contents from a text file, and the data in 2D lists can vary, depending on file content and size, but the structure of file1 i.e dataset2d and file2 i.e partition2d will be the same.</p> <p>Update2: Since Eric asked about how the output should look like.</p> <pre><code> 0.842322 0.94322 0.34232 0.900009 (For A) 0.642322 0.44322 0.24232 0.800009 (For B) </code></pre> <p>This is just an example and the numbers are randomly typed by me. So the first number 0.842322 is the result of applying the formula to column 0 of dataset2d i.e dataset2d[parttionCol][0] for group A having considered rows 1,2,4. </p> <p>The second number, 0.94322 is the result of applying formula to column 1 of dataset2d i.e dataset2d[partitionCol][1] for group A having considered rows 1,2 4. </p> <p>The third number, 0.34232 is the result of applying formula to column 2 of dataset2d i.e dataset2d[partitionCol][2] for group A having considered rows 1,2 4. Similarly we get 0.900009.</p> <p>The first number in second row, i.e 0.642322 is the result of applying the formula to column 0 of dataset2d i.e dataset2d[parttionCol][0] for group B having considered rows 3,5. And so on.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload