[Tutor] Masking operation

Rich Lovely roadierich at googlemail.com
Thu Oct 15 01:31:17 CEST 2009

2009/10/14 Wayne <srilyk at gmail.com>:
> I've searched "Python masking" and various other terms in Google and the
> python docs and came up with nothing useful.
> This is just a bit of a hobby project that was inspired by some assembly
> homework that one of my friends is now doing and I did last semester. The
> assignment was to create a program that took two strings and XOR'ed the one
> by the other.
> For example:
> mask: secret
> data: This is a secret message!
> and you would have:
> s XOR T
> e XOR h
> <skip a few>
> e XOR i
> t XOR s
> <starts at the beginning of the mask>
> s XOR ' '
> So I basically want a mask the same length as my data (in this case, 25), so
> I would have:
> mask = 'secretsecretsecretsecrets'
> The way I'm currently doing it is:
> mask = mask * (len(message)/len(mask))
> for l, m in zip(message, mask):
>     word += chr(ord(l) ^ ord(m))
> but I'm wondering if there are any easier/more pythonic ways.
> Thanks,
> Wayne
> --
> To be considered stupid and to be told so is more painful than being called
> gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
> every vice, has found its defenders, its rhetoric, its ennoblement and
> exaltation, but stupidity hasn’t. - Primo Levi
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
Look up the itertools module:


Specifically, cycle and izip.  itertools.cycle is the trick here, izip
is a more efficient (in this case) version of zip.

Rich "Roadie Rich" Lovely

There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.

More information about the Tutor mailing list