[Tutor] something relevant to array

Rich Lovely roadierich at googlemail.com
Fri Dec 23 11:25:23 CET 2011


On 23 December 2011 10:01, lina <lina.lastname at gmail.com> wrote:
> Hi,
>
> 1] How to input some column in idle
>
> like:
>
> a
> a
> a
>
> 2] I want to combine a series of files like
>
> a.txt
>
> 1 a
> 2 a
> 3 a
>
> b.txt
>
> 1 b
> 2 b
> 3 b
>
> into one as:
> a b
> a b
> a b
>
> The work-in-progress code as following,
>
>
> #!/usr/bin/python3
>
> import os
>
> INFILEEXT = ".xvg"
>
> NUM_OF_FILE = 10
>
> if __name__=="__main__":
>    result = [0]*NUM_OF_FILE
>    for i in range(NUM_OF_FILE):
>
>        filename = "A_mindist_" + str(i+1) + INFILEEXT
>        #text = open(filename,"r").readlines()
>        with open(filename,"r") as f:
>            for line in f:
>                parts = f.readline().strip()
>                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
>
>
> Thanks with best regards,
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

You can't append to an int, you need to initialise your result list
with lists. Instead of

result = [0]*NUM_OF_FILE

you need

result= [ [] for i in range(NUM_OF_FILES) ]

In the future, please include the error message you receive when
trying to run the program.
-- 
Rich "Roadie Rich" Lovely

Just because you CAN do something, doesn't necessarily mean you SHOULD.
In fact, more often than not, you probably SHOULDN'T.  Especially if I
suggested it.

10 re-discover BASIC
20 ???
30 PRINT "Profit"
40 GOTO 10


More information about the Tutor mailing list