[Tutor] Why Doesn't the for loop Increment?

Cameron Simpson cs at cskk.id.au
Tue Jan 12 16:44:15 EST 2021


On 12Jan2021 14:32, Stephen P. Molnar <s.molnar at sbcglobal.net> wrote:
>Python3.7.3 on Debian Buster.
>
>I have a large number of computational docking files from which I wish 
>to extract results.
>
>I have cobbled together a script that works, up to a point:
>
>#!/usr/bin/env python3
># -*- coding: utf-8 -*-
>import pandas as pd
>import numpy as np
>
>df = pd.read_csv('ligands_3')

Did you print df here?

>num = [1,2,3,4,5,6,7,8,9,10]
>
>for ligand in df:

Did you print ligand here?

    
>    for i in num:
>        name_in = "{}.{}.log".format(ligand, i)

I'd write this as:

    name_in = f"{ligand}.{i}.log"

which I find more readable. Not relevant to your problem.

But wait, hang on? What's in your CSV file? It looks like ligands_3 is 
just a list of ligand names? Using pandas.read_csv seems like overkill 
if so. Maybe it does do what you thought.

However, looking at the pandas docs:

    https://pandas.pydata.org/pandas-docs/stable/user_guide/cookbook.html#cookbook-csv

suggests it does what you think it does.

But _print out ligand_! And earlier, print out df! See what they really 
are.

>        data = np.genfromtxt(name_in,skip_header=28, skip_footer=1)
>        name_s = ligand+'-BE'
>        f = open(name_s, 'a')
>        f.write(str(data[0,1])+'\n')
>        f.close()
>
>For each ligand I run ten repetitions of the docking. The ligands_3 
>file contains three ligand names:
>
>7-Phloroeckol
>Aloeemodin
>beta-Sitosterol
>
>The code produces, for example, for the first ligand in the list:
>
>-9.248153586
[...]
>-9.300577161
>
>Which happen to be correct.
>
>The problem, though, is that the script does not go on to the next 
>ligand in the list. Everything that I have found in Google results says 
>that the script will iterate through the list.

The script should iterate through the list. Superficially it looks good 
to me.

Print statements, they are your friend here. See what values you are 
getting at each stage.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list