Aakash
Aakash

Reputation: 5

Array sizes being added instead of staying constant

I wish to add the contents of multiple arrays together in a single array, so I go over different files store the data in an array and then add them, however the data seems to be getting appended rather than just being added. For example I want, a=[1,2,3,4], b=[2,3,4,5],c=[3,4,1,2] and the final array to be sum=[6,9,8,11]. but for some reason I am not able to perform this operation! I know its a stupid mistake somewhere!

x=[]
s=[]
S=[]
p=[]
P=[]
d=[]
D=[]
#print 'startS0 is %f' % S[0]     
#print 'startS1 is %f' % S[1]     

N=3
for i in xrange(1,N+1,1):
        stri = str(i)
        dos_file =open("DOS"+stri,"r")
        #dos_file =open("DOS1","r")
        print 'filename is %s' % dos_file
        for line_aa in dos_file.readlines():
                line_aa=line_aa.split()
                #x=[0]*1000
                #p=[0]*1000
                x.append(line_aa[0])
                s.append(line_aa[1])
                #p.append(line_aa[2])
                #d.append(line_aa[3])
        #dos_file.close()   
        x=[float(i) for i in x]
        s=[float(i) for i in s]
        print 's0 is %f' % s[998]
        print 's1 is %f' % s[999]
        sizes=len(s)
        print 'sizes is %d' % sizes
        S=[0]*sizes
        print 'S0 is %f' % S[0]
        print 'S1 is %f' % S[1]
        sizeS=len(s)
        print 'sizeS is %d' % sizeS
        #dos_file.close()   
        S_tot=[a + b for a, b in zip(s, S)]
        print 'S_tot0 is %f' % S_tot[0]
        print 'S_tot1 is %f' % S_tot[1]
        sizeS_tot=len(s)
        print 'sizeS_tot is %d' % sizeS_tot
        dos_file.close()
        #x=[0]*sizes
        #p=[0]*sizes

print 'endS0 is %f' % S_tot[0]
print 'endS1 is %f' % S_tot[1]

The result I get is:

filename is <open file 'DOS1', mode 'r' at 0x2aaaabe06540>

sizes is 1000

sizeS is 1000

sizeS_tot is 1000
filename is <open file 'DOS2', mode 'r' at 0x2aaaabe065d0>

sizes is 2000

sizeS is 2000

sizeS_tot is 2000
filename is <open file 'DOS3', mode 'r' at 0x2aaaabe06540>

sizes is 3000

sizeS is 3000

sizeS_tot is 3000     

Upvotes: 0

Views: 52

Answers (3)

Aakash
Aakash

Reputation: 5

I figured it out! It was a scope issue. So I need to define the x[],s[] arrays inside the for loop while define the S_tot[], etc. outside it.

Here is the altered code:I am sorry I don't have a lot of comments in the code!

S_tot =[0]*1000
P_tot =[0]*1000
D_tot =[0]*1000
N=3
for i in xrange(1,N+1,1):
        x=[]
        s=[]
        p=[]
        d=[]
        stri = str(i)
        dos_file =open("DOS"+stri,"r")
        #dos_file =open("DOS1","r")
        print 'filename is %s' % dos_file
        for line_aa in dos_file.readlines():
                line_aa=line_aa.split()
                x.append(line_aa[0])
                s.append(line_aa[1])
                p.append(line_aa[2])
                d.append(line_aa[3])
        #dos_file.close()   
        x=[float(i) for i in x]
        s=[float(i) for i in s]
        p=[float(i) for i in p]
        d=[float(i) for i in d]
        #print 's0 is %f' % s[0]     
        #print 's1 is %f' % s[1]     
        #sizes=len(s)
        #print 'sizes is %d' % sizes
        #S_tot=[0]*sizes
        #print 'S0 is %f' % S[0]     
        #print 'S1 is %f' % S[1]     
        #sizeS=len(s)
        #print 'sizeS is %d' % sizeS
        #dos_file.close()   
        #S_tot=[a + b for a, b in zip(s, S_tot)]
        S_tot=[sum(terms) for terms in zip(s, S_tot)]
        P_tot=[sum(terms) for terms in zip(p, P_tot)]
        D_tot=[sum(terms) for terms in zip(d, D_tot)]
        print 'S_tot0 is %f' % S_tot[0]
        print 'S_tot1 is %f' % S_tot[1]
        print 'P_tot1 is %f' % P_tot[1]
        print 'D_tot1 is %f' % D_tot[1]
        dos_file.close()

print 'endS0 is %f' % S_tot[0]
print 'endS1 is %f' % S_tot[1]
print 'endP_tot1 is %f' % P_tot[1]
print 'endD_tot1 is %f' % D_tot[1]
~                                                                                                                                                                                       
~                                                                                                                                                                                       
~                                                                                                                                                                                       
~                                                                                                                                                                                       
~                    

Upvotes: 0

John Coleman
John Coleman

Reputation: 52008

zip is powerful:

>>> a=[1,2,3,4]
>>> b=[2,3,4,5]
>>> c=[3,4,1,2]
>>> sums = [sum(terms) for terms in zip(a,b,c)]
>>> sums
[6, 9, 8, 11]

Upvotes: 0

Alyssa Haroldsen
Alyssa Haroldsen

Reputation: 3731

The operation you want is this:

a = [1, 2, 3, 4]
b = [2, 3, 4, 5]
c = [3, 4, 1, 2]
print([sum(items) for items in zip(a, b, c)])

results in

[6, 9, 8, 11]

Here, I'm using list comprehension, sum, and zip.

Upvotes: 1

Related Questions