添加数组大小而不是保持不变

时间:2015-07-07 23:04:07

标签: python arrays list

我希望将多个数组的内容一起添加到一个数组中,所以我查看不同的文件将数据存储在数组中,然后添加它们,但数据似乎是附加而不是仅仅添加。例如我想要,a = [1,2,3,4],b = [2,3,4,5],c = [3,4,1,2],最后的数组为sum = [6 ,9,8,11]。但由于某种原因,我无法执行此操作!我知道这是一个愚蠢的错误!

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]

我得到的结果是:

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     

3 个答案:

答案 0 :(得分:1)

您想要的操作是:

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)])

结果

[6, 9, 8, 11]

在这里,我使用list comprehensionsumzip

答案 1 :(得分:0)

zip功能强大:

>>> 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]

答案 2 :(得分:0)

我明白了!这是一个范围问题。所以我需要在for循环中定义x [],s []数组,同时在它外面定义S_tot []等。

以下是更改后的代码:很抱歉,代码中没有很多注释!

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]
~                                                                                                                                                                                       
~                                                                                                                                                                                       
~                                                                                                                                                                                       
~                                                                                                                                                                                       
~                    
相关问题