开发者

unexpected list appearing in python loop

I am new to python and have the following piece of test code featuring a nested loop and I'm getting some unexpected lists generated:

import pybel  
import math  
import openbabel  
search = ["CCC","CCCC"]  
matches = []  
#n = 0  
#b = 0  
print search  
for n in search:  
    print "n=",n  
    smarts = pybel.Smarts(n)  
    allmol = [mol for mol in pyb开发者_如何学Cel.readfile("sdf", "zincsdf2mols.sdf.txt")]  
    for b in allmol:  
        matches = smarts.findall(b)  
        print matches, "\n" 

Essentially, the list "search" is a couple of strings I am looking to match in some molecules and I want to iterate over both strings in every molecule contained in allmol using the pybel software. However, the result I get is:

['CCC', 'CCCC']  
n= CCC  
[(1, 2, 28), (1, 2, 4), (2, 4, 5), (4, 2, 28)]   

[]   

n= CCCC  
[(1, 2, 4, 5), (5, 4, 2, 28)]   

[]   

as expected except for a couple of extra empty lists slotted in which are messing me up and I cannot see where they are coming from. They appear after the "\n" so are not an artefact of the smarts.findall(). What am I doing wrong? thanks for any help.


allmol has 2 items and so you're looping twice with matches being an empty list the second time.

Notice how the newline is printed after each; changing that "\n" to "<-- matches" may clear things up for you:

print matches, "<-- matches"
# or, more commonly:
print "matches:", matches


Perhaps it is supposed to end like this

for b in allmol:  
    matches.append(smarts.findall(b))  
print matches, "\n"

otherwise I'm not sure why you'd initialise matches to an empty list

If that is the case, you can instead write

matches = [smarts.findall(b) for b in allmol]
print matches

another possibility is that the file is ending in an empty line

for b in allmol:  
    if not b.strip(): continue
    matches.append(smarts.findall(b))  
    print matches, "\n"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜