[Tutor] how to remove the coming duplication

Andreas Perstinger andreas.perstinger at gmx.net
Thu Nov 10 13:24:07 CET 2011


On 2011-11-10 09:26, lina wrote:
> atoms=[]
>
> def fetchonefiledata(infilename):
>          for line in open(infilename,"r"):
>                  parts=line.strip().split()
>                  atoms=parts[2]
>                  print(atoms[0])

First you define "atoms" as an empty list, but in the line

atoms = parts[2]

you are redefining it by assigning a string to it. Thus, at the end of 
the for-loop, only the last string is stored (in every iteration you 
overwrite the former value with a new one).

You probably want to append the values:

atoms.append(parts[2])

Bye, Andreas


More information about the Tutor mailing list