<div dir="ltr"><div>Hi Ryan,Thank you very much indeed, I'm not sure if I well understood your code, let say, for the example array matrix given represents  H3C-CH3 connection(bonding).</div><div>the result from your code is:</div>
<div>Rows:    [0 0 0 0 1 1 1]  # is these for C indices?</div><div>Columns: [1 2 3 4 5 6 7]   # is these for H indices? but it shouldn't be 6 H's?</div><div>Atomic Distances: [ 1.  1.  1.  1.  1.  1.  1.] # ofcourse this is the number of connections or bonds.</div>
<div><br></div><div>In fact, if I write in the form of dictionary: row indices as keys and column indices as values,</div><div>{0:1, 0:2, 0:3, 0:4, 1:5, 1:6, 1:7}, So, does it mean C[0] is connected to H[1], C[0] is connected to H[2] , H[1],....,C[1] is connected to H[7]?  But I have only 6 H's and two C's  in this example (H3C-CH3) </div>
<div><br></div><div>I have tried some thing like: but still no luck ;(</div><div>import numpy as np</div><div>from collections import defaultdict </div><div>dict = defaultdict(list)</div><div>x=....2d numpy array</div><div>
I = x.shape[0]</div><div>J = x.shape[1]</div><div>d={}</div><div>for i in xrange(0, I, 1): </div><div>  for j in xrange(0, J, 1):</div><div>     if x[i,j] > 0:</div><div>        dict[i].append(j) </div><div># the result is:</div>
<div>dict:  {0: [1, 2, 3, 4], 1: [0, 5, 6, 7], 2: [0], 3: [0], 4: [0], 5: [1], 6: [1], 7: [1]})</div><div>keys: [0, 1, 2, 3, 4, 5, 6, 7]</div><div>values:  [[1, 2, 3, 4], [0, 5, 6, 7], [0], [0], [0], [1], [1], [1]]</div><div>
 </div><div>#The H indices can be found by</div><div> H_rows = np.nonzero(x.sum(axis=1)== 1)  </div><div>result=>H_rows : [2, 3, 4, 5, 6, 7]  # six H's</div><div>I am trying to connect this indices with the dict result but I am confused!</div>
<div>So, now I want to produce a dictionary or what ever to produce results as:  H[2] is connected to C[?]</div><div>                                                                                                                  H[3] is connected to C[?]</div>
<div>                                                                                                                  H[4] is connected to C[?], .....</div><div>Thanks for any help</div><div>                                               .</div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Mar 14, 2013 at 2:26 PM, Ryan <span dir="ltr"><<a href="mailto:rnelsonchem@gmail.com" target="_blank">rnelsonchem@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im"><br>
><br>
> Birda,<br>
><br>
> I think this will get you some of the way there:<br>
><br>
> import numpy as np<br>
> x = ... # Here's your 2D atomic distance array<br>
> # Create an indexing array<br>
> index = np.arange( x.size ).reshape( x.shape )<br>
> # Find the non-zero indices<br>
> items = index[ x != 0 ]<br>
> # You only need the first half because your array is symmetric<br>
> items = items[ : items.size/2]<br>
> rows = items / x.shape[0]<br>
> cols = items % x.shape[0]<br>
> print 'Rows:   ', rows<br>
> print 'Columns:', cols<br>
> print 'Atomic Distances:', x[rows, cols]<br>
><br>
> Hope it helps.<br>
><br>
> Ryan<br>
><br>
> _______________________________________________<br>
> NumPy-Discussion mailing list<br>
> NumPy-Discussion <at> <a href="http://scipy.org" target="_blank">scipy.org</a><br>
> <a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
><br>
<br>
</div>Whoops.<br>
That doesn't quite work. You shouldn't drop half the items array like that.<br>
This will work better (maybe ?):<br>
<div class="im"><br>
import numpy as np<br>
x = ... # Here's your 2D atomic distance array<br>
</div><div class="im">index = np.arange( x.size ).reshape( x.shape )<br>
</div><div class="im">items = index[ x != 0 ]<br>
</div><div class="im">rows = items / x.shape[0]<br>
cols = items % x.shape[0]<br>
</div># This index mask should take better advantage of the array symmetry<br>
mask = rows < cols<br>
print 'Rows:   ', rows[mask]<br>
print 'Columns:', cols[mask]<br>
print 'Atomic Distances:', x[rows[mask], cols[mask]]<br>
<div class="HOEnZb"><div class="h5"><br>
Ryan<br>
<br>
<br>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
</div></div></blockquote></div><br></div>