XORing long strings opimization?

rm roel.mathys at yucom.be
Wed Nov 5 11:15:33 EST 2003


Skip Montanaro <skip at pobox.com> wrote in message news:<mailman.448.1067985794.702.python-list at python.org>...
> Noen>   alist = [chr(x ^ y) for x in ord1 for y in ord2]
> 
>     Noen> This creates a way too big list... Im not familiar with two for
>     Noen> loops in one, so I cant see whats wrong :(
> 
> Try:
> 
>     alist = [chr(x ^ y) for (x, y) in zip(ord1, ord2)]
> 
> Your listcomp nests two for loops.  You need to iterate over both ord1 and
> ord2 at the same time.
> 
> Skip

this could work as well:

import itertools

alist = [ chr(x^y) for x,y in itertools.izip( itertools.imap(ord,a) ,
itertools.imap(ord,b)) ]

if I'm not mistaken this will pull out 1 character at a time, maybe
not fastest method, but you won't have multiple copies of the list
(original or transformed), and when you're comparing big files this
might be advisable

rm




More information about the Python-list mailing list