XORing two lists

Dale Strickland-Clark dale at riverhall.NOTHANKS.co.uk
Tue Feb 18 19:44:38 EST 2003


Boris Genz <boris_genz at hotmail.com> 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

How about something like this:

# Make sure longer list is in a and extend
# shorter list with zeros. (Works if same length.)

if len(a) < len(b): a, b = b, a 
[ x ^ y for x, y in zip(a, b + [0]*(len(a)-len(b)))]
--
Dale Strickland-Clark
Riverhall Systems Ltd




More information about the Python-list mailing list