Nerija .Alcin Nerija .Alcin - 1 year ago 99
Python Question

How to do a loop selecting only the first values of an array?

Here's my code

import numpy as np
contrainte1= 1080*0.65 # minutes tous les jours
contrainte2= 720*0.55 # minutes du lundi au vendredi
X=np.array([[9, 48],[12, 46],[14, 41],[12, 45],[6, 50],[10, 48],[25, 12],[26, 15],[30, 10],[31, 16],[40, 10],[41, 12],[35, 18],[35, 19],[30, 21],[28, 21],[25, 24],[21, 28],[22, 30],[25, 26],[26, 25],[30, 20],[32, 16],[35, 12]])<
#La première position représente la contrainte 1
#La deuxième position représente la contraire 2

# contrainte 1
for i in X[-19:]:
print(i)
for y in #This is where I need help


I want to do a loop for each array(i) where I will do the sum of every value in the first section of my array. Per example I want to be able to do the sum of 9+12+14....+35 with np.sum()

When I try indexing like SciPy showed me I can't seem to find the technique to do what I want. Or I just dont understand(language barrier)

Thanks

Answer Source

If I understood you, you just want to sum elements in the first column? All that needs is a little indexing and sum:

In [19]: X[:, 0].sum()
Out[19]: 600
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download