Home > Mobile >  Python Conditions: IF not anything I selected, then do this
Python Conditions: IF not anything I selected, then do this

Time:08-05

I'm parsing yaml files and append my structures ID based on what is written in the section 'Organism", then I'd like to sort my structures based on type of organism. If the file doesn't contain any of my mentioned organisms, then I want to append it to the list "other".

PDB_ID = ['1NKW', '7TOS'] #1NKW is Deinococcus radiodurans, 7TOS is E.coli

ecoli =[]
deino=[]
other=[]
for ID in PDB_ID:    
    yaml_file = "{}.yaml" .format(ID)
    
    with open(yaml_file) as stream:
        doc = yaml.load(stream, Loader=yaml.FullLoader)
        bac=doc["Organism"]
                
        a = "coli"
        c="radiodurans"
                
        if a in bac:
            ecoli.append("{}" .format(ID))
        if b in bac:
            deino.append("{}" .format(ID))
        if (not a or not b) in bac:
            other.append("{}" .format(ID))
        else:
            continue

The lists "ecoli" and "deino" are correct because "ecoli" contains only 7TOS and "deino" contains only 1NKW. However, the list "other" is incorrect because it should be empty (as both of the structures have one of my mentioned organisms) but it contains 1NKW (this structure is deinococcus and it is already in the list "deino"). I also tried it with this code:

        if a in bac:
            ecoli.append("{}" .format(ID))
        if b in bac:
            deino.append("{}" .format(ID))
        else:
            other.append("{}" .format(ID))

But as I assumed it is incorrect as well because list "others" now contains both structures.

I'd like to know what is the correct form of using "if" or "if not" expressions here? The goal is to have list "other" with IDs which don't contain "a" or "b" (so it is not E.coli or Deinococcus).

CodePudding user response:

(not a or not b) in bac is not doing what you want it to do. You want a not in bac and b not in bac

Or, just use if/elif/else:

if a in bac:
    ecoli.append("{}".format(ID))
elif b in bac: # Rather than `if b in bac`
    deino.append("{}".format(ID))
else:
    other.append("{}".format(ID))
  • Related