Search code examples
pythonarraysvariablesintrospection

get an array variable in python


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!


Solution

  • 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()