2010-03-10 6 views
0

puis-je le faire dans une boucle, en produisant le nom du fichier à partir du nom du tableau à stocker?obtenir une variable tableau en 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 

nous vous remercions de votre aide!

Répondre

1

Une façon est d'avoir a, b, c, d, e, f dans un dict. Alors vous feriez juste quelque chose comme:

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() 
+0

Ils sont dans un dict - il s'appelle * locals() * :) –

+1

@Ian Oui, mais certaines personnes n'aiment pas utiliser cela parce que c'est un peu un hack. –

+0

merci! le fait-il si a, b, c, d, e, f sont aussi des tableaux? – lclevy

2

En supposant que vous stockez tous les tableaux intermédiaires pour une raison.

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 

Ou quelque chose comme ça devrait fonctionner trop

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

Ne calcule-t-on pas 'map (operator.xor, a, b)' à plusieurs reprises? –

+0

@ Mark Dickinson, oui c'était. Je l'ai réparé maintenant :) –

Questions connexes