XORing two lists

Andrew Wilkinson ajw126NO at SPAMyork.ac.uk
Tue Feb 18 18:44:47 EST 2003


The easy way would be something like...

new = []
for i in range(min(len(a), len(b)):
    new.append(a[i] ^ b[i])
new += a[len(b):] + b[len(a):]

or, removing the for loop...

new = map(lambda x: x[0] ^ x[1], zip(a, b)) + a[len(b):] + b[len(a):]

Both these assume that the initial two lists are in a and b and the output
list in is new.

I can't think of a better way to append the tail of the longer list at the
moment, I'm sure someone will point out the error of my ways though!

HTH,
Andrew

Boris Genz wrote:
> Hello Guys...
> I am making my second ( hehe, yeah second, I'm a real newb, but I'm
> trying really hard ) python program and I have small problems. I have
> two text files, in each I have a list ( of numbers ) written ( with
> pickle command, because I want it to be a list after saving it to
> file and loading it back to program )... What I want for my program
> to do is to read the information from both text files, retreive the
> lists from them, and make XOR operation with each number from my
> list. So for example, in file test1.txt I have a following list:
> [1234, 65477, 44412, 2214]
> and in the second file called test2.txt I have something like this:
> [78974, 2221, 4447]
> I want to get a list with following numbers ( I XORed each pair of the
> numbers manually. The last number is the same, because it doesn't
> have its pair ):
> [77996,63336,48163,2214]
> Can you tell me how to do this...
> I would really appreciate your help.
> Thanks in advance.
> Boris








More information about the Python-list mailing list