0

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?

3
  • what's the points of the loop(s) if you're overwriting your cost again and again? Commented Feb 28, 2017 at 17:36
  • You can use numpy arrays to obtain elements from index masks. Commented Feb 28, 2017 at 17:38
  • hello ..i am using a for loop to run all the elements in the lists..i just want to use a different function for index 3,6,9..i just don't know how to do that.. Commented Feb 28, 2017 at 17:55

3 Answers 3

1

try this:

[x+y+z for x,y,z in zip(a_cost, b_cost, [y if x in (3,6,9) else 0 for x,y in enumerate(c_cost)])]

So basically you have three lists a,b,c. But for c you only want to add if the index is 3,6 or 9.

There is a special way to do this, by using enumerate. This will basically pair all elements of a list with that elements index. Try this to see what it looks like:

[(x,y) for x,y in enumerate(c_cost)]

Now, you only want to add the values for index 3,6,9. A trick to doing this is to add 0 if the index of that element is not 3,6 or 9. Let's see what this looks like.

 [y if x in (3,6,9) else 0 for x,y in enumerate(c_cost)]

Great now we can zip this list together with a_cost and b_cost. When we add the elements from c_cost that dont have index 3,6 or 9 we add 0 to the sum of a_cost and b_cost.

On a final note, in order to get your final value of 11000 you need to build your initial lists need to be through range(11) :

a=[i for i in range(11)] #etc
Sign up to request clarification or add additional context in comments.

Comments

0

Why not solve the problem in the c_cost list? If you use only some elements, don't generate the rest.

a=[i for i in range(11)]
a_cost=[1000*i for i in a] 
b=[i for i in range(11)] 
b_cost=[100*i for i in b]
c=[i for i in range(11)]
c_cost=[50*i if i%3 == 0 else 0 for i in c]

cost = [a_cost[i] + b_cost[i] + c_cost[i] for i in range(1, 11)]

print cost

If, for some reason you haven't mentioned, you can't change the cost lists, you can still use a conditional addition:

cost = [a_cost[i] + b_cost[i] for i in range(len(a_cost))]
for i in range(0, len(a_cost), 3):
    cost[i] += c_cost[i]

Comments

0

trying to determine your intent from the code, it looks like you want to change the terms summed depending on index odd/even?

[sum(abc[:3-(i%2==0)]) for i, abc in enumerate(zip(a_cost, b_cost, c_cost))][1:]
Out[21]: [1150, 2200, 3450, 4400, 5750, 6600, 8050, 8800, 10350]

2 Comments

Dear all , thank you so much for your help. The answer that helped in this occasion was EoinS . I need to apologize for the other friends because the lists i provided was just a sample and may mislead them for what i really wanted. My concern was to use the costs in the real lists according to the index which was appopriate for the time. The overall code will estimate the costs for a turboshaft engine during a period of 40 years. During its lifetime it will be inducted several times for major repairs and all these costs sum up and need to be considered at the operators Budget.
The community really helped me and i hope to help someone in the future like you guys did. Thanks again

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.