views:

81

answers:

2

hi, 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!

+2  A: 

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
gnibbler
Doesn't this just compute `map(operator.xor, a, b)` repeatedly?
Mark Dickinson
@Mark Dickinson, yes it was. I fixed it now :)
gnibbler
+1  A: 

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
They are in a dict -- it's called *locals()* :)
Ian Clelland
@Ian Yes, but some people don't like to use that because it is kind of a hack.
Justin Peel
thank you! does it if a,b,c,d,e,f are also arrays ?
lclevy
The way I was doing it was to have mydict['a'] return the array a. You can use locals() by just replacing mydict with locals().
Justin Peel
thank you a lot!!!
lclevy