0

Suppose I have made a large list of numbers, and I want to make another one which I will add, pairwise, with the first list.

Here's the first list, A:

[109, 77, 57, 34, 94, 68, 96, 72, 39, 67, 49, 71, 121, 89, 61, 84, 45, 40, 104, 68, 54, 60, 68, 62, 91, 45, 41, 118, 44, 35, 53, 86, 41, 63, 111, 112, 54, 34, 52, 72, 111, 113, 47, 91, 107, 114, 105, 91, 57, 86, 32, 109, 84, 85, 114, 48, 105, 109, 68, 57, 78, 111, 64, 55, 97, 85, 40, 100, 74, 34, 94, 78, 57, 77, 94, 46, 95, 60, 42, 44, 68, 89, 113, 66, 112, 60, 40, 110, 89, 105, 113, 90, 73, 44, 39, 55, 108, 110, 64, 108]

And here's B:

[35, 106, 55, 61, 81, 109, 82, 85, 71, 55, 59, 38, 112, 92, 59, 37, 46, 55, 89, 63, 73, 119, 70, 76, 100, 49, 117, 77, 37, 62, 65, 115, 93, 34, 107, 102, 91, 58, 82, 119, 75, 117, 34, 112, 121, 58, 79, 69, 68, 72, 110, 43, 111, 51, 102, 39, 52, 62, 75, 118, 62, 46, 74, 77, 82, 81, 36, 87, 80, 56, 47, 41, 92, 102, 101, 66, 109, 108, 97, 49, 72, 74, 93, 114, 55, 116, 66, 93, 56, 56, 93, 99, 96, 115, 93, 111, 57, 105, 35, 99]

How might I generate the arithmetic addition logic, processing each pairwise value one by one (A[0] and B[0], through A[99], B[99]) and producing the list C (A[0] + B[0] through A[99]+ B[99])?

1
  • 1
    thanks for the corrections of my english words .. Commented Nov 14, 2011 at 3:27

3 Answers 3

5
result = [(x + y) for x, y in itertools.izip(A, B)]

Or:

result = map(operator.add, itertools.izip(A, B))
Sign up to request clarification or add additional context in comments.

4 Comments

operator.add may be a better choice than sum
@ but if I want to add the the modular addition(ex, 125) and how about the result? by using result = [(x + y) for x, y in itertools.izip(A, B) %125] dosen't works
@haeaohoh: [(x + y) % 125 for x, y in itertools.izip(A, B)]. The expression that works on the elements is always before for.
@CatPlusPlus, Ok I understand now, I just wanna make one time pad encryption tool for this questions, thanks :)
2

Here is two possible options:

  • Use list comprehension.
  • Use NumPy.

I will be using shortened versions of your lists for convenience, and the element-wise sum will go into c.

List comprehension

a = [109, 77, 57, 34, 94, 68, 96]
b = [35, 106, 55, 61, 81, 109, 82]
c = [a_el + b_el for a_el,b_el in zip(a, b)]

NumPy

import numpy as np

a = np.array([109, 77, 57, 34, 94, 68, 96])
b = np.array([35, 106, 55, 61, 81, 109, 82])
c = a + b

4 Comments

Umh, and then if add the modular addition (ex 125) does c = ((a+b)%125) will works?
@haeaohoh Yes, for the NumPy option, you can do (a + b) % 125. It would be good to edit your question to include that requirement, if you are looking for that.
@haeaohoh Of course, the list comprehension could also be modified to do the modular addition: c = [(a_el + b_el) % 125 for a_el,b_el in zip(a, b)].
Thanks pretty works well, this shuold be useful for One time Pad algorithms
0

With a list comprehension:

C = [A[i]+ B[i] for i in range(len(A))]

And even safer:

C = [A[i]+ B[i] for i in range(len(A)) if len(A) == len(B)]

3 Comments

the code seems rather complicate for me C = [A[i]+ B[i] for i in range(len(A)) if len(A) == len(B)]
Then don't use it. I'm just providing it to be complete, i.e. the first line only works and assumes that the lengths of both lists is equal, but that may not always be the case. Same for most of the other solutions posted.
No problems mate the meantings you've helped me a lots, thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.