Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use <a href="http://www.numpy.org" rel="nofollow">Numpy</a> (I hope this is not fancy for you):</p> <pre><code>import numpy 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] ] dataset2D_size = dataset2D[0] dataset2D = numpy.array(dataset2D) partition2D = [ ['A', '1', '2', '4'], ['B', '3', '5'], ['C', '6'] ] for partition in partition2D: label = partition[0] row_indices = [int(i) for i in partition[1:]] # Take the specified rows rows = dataset2D[row_indices] # Iterate the columns (this is the power of Python!) for column in zip(*rows): # Now, column will contain one column of data from specified row indices print column, # Apply your formula here print </code></pre> <p>or <strong>if you don't want to install Numpy</strong>, here is what you can do (this is what you want, actually):</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] ] partition2D = [ ['A', '1', '2', '4'], ['B', '3', '5'], ['C', '6'] ] dataset2D_size = dataset2D[0] for partition in partition2D: label = partition[0] row_indices = [int(i) for i in partition[1:]] rows = [dataset2D[row_idx] for row_idx in row_indices] for column in zip(*rows): print column, print </code></pre> <p>both will print:</p> <pre> (0, 1, 1) (0, 0, 1) (0, 2, 1) (1, 0, 0) (2, 0) (2, 0) (0, 1) (1, 1) (1,) (0,) (2,) (1,) </pre> <p><strong>Explanation of second code (without Numpy)</strong>:</p> <pre><code>[dataset2D[row_idx] for row_idx in row_indices] </code></pre> <p>This is basically you take each row (<code>dataset2D[row_idx]</code>) and collate them together as a list. So the result of this expression is a list of lists (which comes from the specified row indices)</p> <pre><code>for column in zip(*rows): </code></pre> <p>Then <code>zip(*rows)</code> will <strong>iterate column-wise</strong> (the one you want). This works by taking the first element of each row, then combine them together to form a <a href="http://docs.python.org/release/1.5.1p1/tut/tuples.html" rel="nofollow">tuple</a>. In each iteration, the result is stored in variable <code>column</code>.</p> <p>Then inside the <code>for column in zip(*rows):</code> you already have your intended column-wise iterated elements from specified rows!</p> <p>To apply your formula, just change the <code>print column,</code> into the stuff you wanna do. For example I modify the code to include row and column number:</p> <pre><code>print 'Processing partition %s' % label for (col_num, column) in enumerate(zip(*rows)): print 'Column number: %d' % col_num for (row_num, element) in enumerate(column): print '[%d,%d]: %d' % (row_indices[row_num], col_num, element) </code></pre> <p>which will result in:</p> <pre> Processing partition A Column number: 0 [1,0]: 0 [2,0]: 1 [4,0]: 1 Column number: 1 [1,1]: 0 [2,1]: 0 [4,1]: 1 Column number: 2 [1,2]: 0 [2,2]: 2 [4,2]: 1 Column number: 3 [1,3]: 1 [2,3]: 0 [4,3]: 0 Processing partition B Column number: 0 [3,0]: 2 [5,0]: 0 Column number: 1 [3,1]: 2 [5,1]: 0 Column number: 2 [3,2]: 0 [5,2]: 1 Column number: 3 [3,3]: 1 [5,3]: 1 Processing partition C Column number: 0 [6,0]: 1 Column number: 1 [6,1]: 0 Column number: 2 [6,3]: 2 Column number: 3 [6,3]: 1 </pre> <p>I hope this helps.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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