Reputation: 119
I have the following problem
import random
a_list = range(0,64)
a = random.sample(a_list,16)
print(a)
So I get 16 random numbers.
Now what I want further is to add every two numbers in the random set of 16 elements even with each number to be added with itself.
Upvotes: 0
Views: 121
Reputation: 800
If you want to use just pure python lists, the solution can be a one-liner list comprehension.
sums_list = [n+m for n in a for m in a]
With Numpy you can use broadcasting.
import numpy as np
arr = np.array(a)
sums = arr[:, np.newaxis] + arr[np.newaxis, :]
sums_list = sums.flatten().tolist()
Upvotes: 1
Reputation: 117
Can you please write down an example of what you are trying to achieve?
Upvotes: 0