mardi 28 février 2017

How i can add values from multiple lists using a for and if loop in Python?

I am using 3 lists and want to add the lists values according to a special condition. For a range of 10 items i need to a. Add the values at lists A and B using the values with index 1,2,4,5,7,8,10 b. Add the values at list A,B and C using the values with index 3,6,9

The code i wrote is the following :

Create lists

a=[i for i in range(10)]
a_cost=[1000*i for i in a] 
b=[i for i in range(10)] 
b_cost=[100*i for i in b]
c=[i for i in range(10)]
c_cost=[50*i for i in c]

code to estimate costs

for i in range(1,11):
   for i in range(1,11,2):
      cost=[x1+x2+x3 for x1,x2,x3 in zip(a_cost,b_cost,c_cost)]
   else: cost=[x1+x2 for x1,x2 in zip(a_cost,b_cost)]

When i run the code i am getting this result

cost=[1100, 2200,3300,4400,5500,6600,7700,8800,9900,11000]

and i should be getting this result

cost=[1100, 2200,3450,4400,5500,6900,7700,8800,10350,11000]

I also used the for following loop but still get the same results

for i in range(1,11):
   if i==i+2:
      d=[x1+x2+x3 for x1,x2,x3 in zip(a_cost,b_cost,c_cost)]
   else: d=[x1+x2 for x1,x2 in zip(a_cost,b_cost)]

Can someone please provide an insight on that issue?

Aucun commentaire:

Enregistrer un commentaire