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!

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

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

3 Comments

They are in a dict -- it's called locals() :)
@Ian Yes, but some people don't like to use that because it is kind of a hack.
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().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.