[Tutor] something relevant to array

Alan Gauld alan.gauld at btinternet.com
Fri Dec 23 11:42:44 CET 2011


On 23/12/11 10:01, lina wrote:

>          with open(filename,"r") as f:
>              for line in f:
>                  parts = f.readline().strip()

Are you sure you want to do this?
You are already reading a line from the file in the for loop.
This will read the next line. So parts will comprise every second line 
in the file. Is that what you want?


>                  if len(parts) == 26:
>                      dist = parts.split()[1]
>                      print(dist)
>                      result[i].append(dist)
>      print(result)
>
> $ cat A_mindist_1.xvg
> 4.640000e+05  3.169008e-01
> 4.680000e+05  4.319328e-01
> 4.720000e+05  5.126960e-01
>
>
> $ cat A_mindist_2.xvg
> 4.640000e+05  5.237660e-01
> 4.680000e+05  2.352828e-01
> 4.720000e+05  2.280239e-01
>
>
> I wish
> result[0]  =
> 3.169008e-01
> 4.319328e-01
> 5.126960e-01
>
> result[1] =
> 5.237660e-01
> 2.352828e-01
> 2.280239e-01

This suggests you want each line so i'd expect your code to look more like

          with open(filename,"r") as f:
              for line in f:
                  result[i].append(line.strip().split()[1])


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list