在Python中获取数组变量
我可以在一个循环里做到这一点吗?通过数组的名字来生成要存储的文件名?
ab = array.array('B', map( operator.xor, a, b ) )
f1 = open('ab', 'wb')
ab.tofile(f1)
f1.close
ac = array.array('B', map( operator.xor, a, c ) )
f1 = open('ac', 'wb')
ac.tofile(f1)
f1.close
ad = array.array('B', map( operator.xor, a, d ) )
f1 = open('ad', 'wb')
ad.tofile(f1)
f1.close
ae = array.array('B', map( operator.xor, a, e ) )
f1 = open('ae', 'wb')
ae.tofile(f1)
f1.close
af = array.array('B', map( operator.xor, a, f ) )
f1 = open('af', 'wb')
af.tofile(f1)
f1.close
谢谢你的任何帮助!
2 个回答
2
假设你是为了某个原因在存储所有的中间数组。
A={}
for v,x in zip((b,c,d,e,f),'bcdef'):
fname = 'a'+x
A[fname] = (array.array('B', map( operator.xor, a, v ) ))
f1 = open(fname, 'wb')
A[fname].tofile(f1)
f1.close
或者类似这样的方式也可以奏效。
A={}
for x in 'bcdef':
fname = 'a'+x
A[fname] = (array.array('B', map(a.__xor__, vars()[x] ) ))
f1 = open(fname, 'wb')
A[fname].tofile(f1)
f1.close
1
一种方法是把a、b、c、d、e、f放在一个字典里。然后你可以像这样做:
for x in 'bcdef':
t = array.array('B', map( operator.xor, mydict['a'], mydict[x] ) )
f1 = open(''.join('a',x),'wb')
t.tofile(f1)
f1.close()