<div dir="ltr">Oh, thanks alot. can the " <span class="" style="font-family:arial,sans-serif;font-size:13px">atoms = np.array(['C1', 'C2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8'])" able to make  general? I mean, if I have a big molecule, it seems difficult to label each time. Ofcourse I'm new to python(even for programing) and I didn't had any knowhow about pandas, but i will try it. </span><span class="" style="font-family:arial,sans-serif;font-size:13px">any ways, it is great help, many thanks Ryan</span><div>
<span class="" style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span class="" style="font-family:arial,sans-serif;font-size:13px">Birda</span></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Thu, Mar 14, 2013 at 5:03 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">Birdada Simret <birdada85 <at> <a href="http://gmail.com" target="_blank">gmail.com</a>> writes:<br>
<br>
><br>
><br>
><br>
</div><div><div class="h5">> Hi Ryan,Thank you very much indeed, I'm not sure if I well understood your<br>
code, let say, for the example array matrix given represents  H3C-CH3<br>
connection(bonding).<br>
> the result from your code is:<br>
> Rows:    [0 0 0 0 1 1 1]  # is these for C indices?<br>
> Columns: [1 2 3 4 5 6 7]   # is these for H indices? but it shouldn't be 6 H's?<br>
> Atomic Distances: [ 1.  1.  1.  1.  1.  1.  1.] # ofcourse this is the number<br>
of connections or bonds.<br>
><br>
> In fact, if I write in the form of dictionary: row indices as keys and column<br>
indices as values,<br>
> {0:1, 0:2, 0:3, 0:4, 1:5, 1:6, 1:7}, So, does it mean C[0] is connected to<br>
H[1], C[0] is connected to H[2] , H[1],....,C[1] is connected to H[7]?  But I<br>
have only 6 H's and two C's  in this example (H3C-CH3) <br>
><br>
> I have tried some thing like: but still no luck ;(<br>
> import numpy as np<br>
> from collections import defaultdict <br>
> dict = defaultdict(list)<br>
> x=....2d numpy array<br>
><br>
> I = x.shape[0]<br>
> J = x.shape[1]<br>
> d={}<br>
> for i in xrange(0, I, 1): <br>
>   for j in xrange(0, J, 1):<br>
>      if x[i,j] > 0:<br>
>         dict[i].append(j) <br>
> # the result is:<br>
> dict:  {0: [1, 2, 3, 4], 1: [0, 5, 6, 7], 2: [0], 3: [0], 4: [0], 5: [1], 6:<br>
[1], 7: [1]})<br>
> keys: [0, 1, 2, 3, 4, 5, 6, 7]<br>
> values:  [[1, 2, 3, 4], [0, 5, 6, 7], [0], [0], [0], [1], [1], [1]]<br>
><br>
>  <br>
> #The H indices can be found by<br>
>  H_rows = np.nonzero(x.sum(axis=1)== 1)  <br>
> result=>H_rows : [2, 3, 4, 5, 6, 7]  # six H's<br>
> I am trying to connect this indices with the dict result but I am confused!<br>
> So, now I want to produce a dictionary or what ever to produce results as:<br>
 H[2] is connected to C[?]<br>
>                                                                              <br>
                                    H[3] is connected to C[?]<br>
>                                                                              <br>
                                    H[4] is connected to C[?], .....<br>
> Thanks for any help<br>
>                                                .<br>
><br>
><br>
</div></div>> On Thu, Mar 14, 2013 at 2:26 PM, Ryan <rnelsonchem <at> <a href="http://gmail.com" target="_blank">gmail.com</a>> wrote:<br>
><br>
><br>
<br>
Birda,<br>
<br>
I don't know how your getting those values from my code. Here's a slightly<br>
modified and fully self-contained version that includes your bonding matrix:<br>
<br>
import numpy as np<br>
x = np.array(<br>
        [[ 0., 1.54, 0., 0., 0., 1.08, 1.08, 1.08 ],<br>
        [ 1.54, 0., 1.08, 1.08, 1.08,  0.,  0.,  0. ],<br>
        [ 0., 1.08, 0., 0., 0., 0., 0., 0. ],<br>
        [ 0., 1.08, 0., 0., 0., 0., 0., 0. ],<br>
        [ 0., 1.08, 0., 0., 0., 0., 0., 0. ],<br>
        [ 1.08, 0., 0., 0., 0., 0., 0., 0. ],<br>
        [ 1.08, 0., 0., 0., 0., 0., 0., 0. ],<br>
        [ 1.08, 0., 0., 0., 0., 0., 0., 0. ]]<br>
             )<br>
atoms = np.array(['C1', 'C2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8'])<br>
<div class="im">index = np.arange( x.size ).reshape( x.shape )<br>
items = index[ x != 0 ]<br>
rows = items / x.shape[0]<br>
cols = items % x.shape[0]<br>
</div><div class="im">mask = rows < cols<br>
print 'Rows:   ', rows[mask]<br>
print 'Columns:', cols[mask]<br>
</div>print 'Bond Atom 1:     ', atoms[ rows[mask] ]<br>
print 'Bond Atom 2:     ', atoms[ cols[mask] ]<br>
<div class="im">print 'Atomic Distances:', x[rows[mask], cols[mask]]<br>
<br>
</div>If I copy that into a file and run it, I get the following output:<br>
<div class="im"><br>
Rows:    [0 0 0 0 1 1 1]<br>
</div>Columns: [1 5 6 7 2 3 4]<br>
Bond Atom 1:      ['C1' 'C1' 'C1' 'C1' 'C2' 'C2' 'C2']<br>
Bond Atom 2:      ['C2' 'H6' 'H7' 'H8' 'H3' 'H4' 'H5']<br>
Atomic Distances: [ 1.54  1.08  1.08  1.08  1.08  1.08  1.08]<br>
<br>
Honestly, I did not think about your code all that much. Too many 'for' loops<br>
for my taste. My code has quite a bit of fancy indexing, which I could imagine<br>
is also quite confusing.<br>
<br>
If you really want a dictionary type of interface that still let's you use Numpy<br>
magic, I would take a look at Pandas (<a href="http://pandas.pydata.org/" target="_blank">http://pandas.pydata.org/</a>)<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>