How to sum list values ββby the power of their indices in Python 3 ?
Example:
[3, 0, 2] = 3^1 + 0^2 + 2^3 = 11
The idea is to create a unique index for any possible combination of non-negative numbers in the list. That way I can use a list to calculate the index of something.
Edit: while the question was answered, I just realized that the method does not create a unique index for any combination of non-negative integers in the list. For this, if a is the number of possible integers and is based on the accepted answer,
sum(a ** i * j for i,j in enumerate(l, 0))
The idea is that each number will increase the index by an amount exponentially proportional to its position in the list. Assuming a=4 ( 0 to 3 ), the above example becomes
[3, 0, 2] = 4^0*3 + 4^1*0 + 4^2^2 = 35
If the indices will vary from 0 to 4^3-1=63 .
source share