0

can I do this in a loop, by producing the file name from the name of the array to store ?

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

thank you for any help!

Facundo Casco
  • 10,065
  • 8
  • 42
  • 63
lclevy
  • 23
  • 1
  • 6

2 Answers2

2

Assuming you are storing all the intermediate arrays for a reason.

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

Or something like this should work too

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
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
1

One way is to have a,b,c,d,e,f in a dict. Then you'd just do something like:

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()
Justin Peel
  • 46,722
  • 6
  • 58
  • 80