Add indexes of a vector respecting condition

0

I have the following code:

import random

usuarios = 2
APs = 2
distancias = random.sample(range(0, 100), usuarios*APs)
teste = [sum(distancias[x: x + usuarios]) for x in range(0, len(distancias), usuarios)]

print(teste)

This code creates a vector with the size of the multiplication of the APs with the users and then adds the sum of 2 in 2 and saves it in the test vector ..

For example:

  • Let's say the vector looks like this:

    distances = [10, 20, 40, 60]

  • The result will look like this:

    test = [30, 100]

However, what I want is for the sum to be made only of values smaller than 50, if it is greater, the value is assigned to the previous or subsequent sum.

For example:

In the values given above the answer should be:

teste = [90, 40]
  • Notice that the value 60 was added to the previously added part because it is greater than 50.

Sorry if it was not very clear, I tried to be as much as possible.

Can anyone help me with this problem? I can not go on.

    
asked by anonymous 21.12.2017 / 02:17

1 answer

0

I think the best thing for you would be to transform the array into a 2-dimensional array and then add the pairs.

An example using numpy:

import random
import numpy as np

# tamanho de cada um dos novos arrays pequenos
usuarios = 2
# convertendo em um array do numpy
distancias = np.array([1,2,3,4,5,6,7,8,9,10])
print("distancias = ")
print(distancias)
# transformando o array de 10 valores em um array de 5 valores
# onde cada um desses valores eh um array com 2 valores
matrix = distancias.reshape((distancias.size // usuarios),usuarios)
print("matrix = ")
print(matrix)
# soma cada um desses arrays pequenos, 
# criando um array array das somas
nova = matrix.sum(axis=1)
print("nova = ")
print(nova) # [ 3  7 11 15 19]

You can see this example running here: link

    
21.12.2017 / 03:16