1

What would be the best implementation for multiplying a list of numbers raised to a defined number of exponents.

Currently I have a list of 8 numbers that would be needed to be raised to a power. Every list will always contain 8 numbers and the numbers in the list will always have the exponent of which their index value is.

For example:

List = [1,2,3,4,5,6,7,8]

Power(1,0) + Power(2,1) + Power(3,2) +.... Power(8,7)    

However, the issue is what if the list has a none value, how can you carry the exponential increased value without affecting the total sum.

Example:

List = [1,None,3,4,5,6,7,8]

Power(1,0) + (none) + Power(3,2) +.... Power(8,7)

Any ideas of implementation would help.

6
  • 1
    Are the numbers and powers always in order like that? Or is the power always one less than the number to be powered? Commented Dec 26, 2018 at 15:59
  • 1
    Sorry, I didn't completely understand. Each value will be powered by a different number? Commented Dec 26, 2018 at 15:59
  • @StephenCowley There will always be 8 numbers (which will vary in value) in the list and the first exponent with the first number in the list will begin at 0. So the number in index 0, will have a power of 0. The second number at index 1 will have a power of 1 and so on.. Commented Dec 26, 2018 at 16:07
  • @HelenaMartins Each value should be powered in consecutive order starting at 0. The first number in the list should be powered by 0, the second number should be powered by 1, the third number should be powered by 2... However if the the third item in the list is 'None' then you should still continue with the consecutive order for the next item in the list. Continuing the sequence the 4 item in the list should have a power of 3. Commented Dec 26, 2018 at 16:12
  • Got it! Below is my answer to your question :) In case of doubt, just give me a hint Commented Dec 26, 2018 at 16:12

3 Answers 3

1

Maybe something like this could get you started?

import numpy as np

l = [1, None, 3, 4, 5, 6, 7, 8]

# If you need to define the powers
# powers = [0, 1, 2, 3, 4, 5, 6, 7]

powers = np.arange(len(l)) # If powers are always indices

arr = np.array([x if x is not None else 0 for x in l])

arr**powers
# array([      1,       0,       9,      64,     625,    7776,  117649, 2097152]

(arr**powers).sum()
# 2223276

On second thought, the above would have a problem if you had [None, 1, 2, 3] since 0**0=1. So we should probably go with something like

l = [1, None, 3, 4, 5, 6, 7, 8]

numbers = np.array([x for x in l if x is not None])
powers = np.array([i for i in range(len(l)) if l[i] is not None])
(numbers**powers).sum()
Sign up to request clarification or add additional context in comments.

3 Comments

What if the first item at index 0 is a none value then it will return 1. Would the best way to fix this to be by checking index 0 for a 'None' Value then assigning a 0?
@Dom_Codes You're exactly right. The second part of my answer addresses that
I'd just suggest to replace the powers = np.arange(8) with powers = np.arange(len(l)). More versatile and less hard code ;)
1
List = [1,None,3,4,5,6,7,8]
result = sum([pow(List[i],i) for i in range(len(List)) if str(List[i]).isdigit()])

Comments

0

Well, you can use list comprehension to only pow numeric values:

index_list = range(len(list))

sum([pow(list[i], i) for i in index_list if str(list[i]).isdigit()])
#Output: 2223276

In here, we sum a list that contains all values powered by an exponent. It will only sum numeric values!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.