-8

So if the input is [3, 2, 5], the output would be [1, 2, 25], and if the input was [5, 10, 20], the output would be [1, 10, 400] (5^0, 10^1, 20^2)

Georgy
  • 12,464
  • 7
  • 65
  • 73

5 Answers5

0

You could just use a map between your input list and a range.

def raise_power(input_list):
    return list(map(lambda x,y: x**y, input_list, range(len(input_list))))

Or a more straightforward way (suggested by @roganjosh) to put it could be :

def raise_power(input_list):
    return [i**x for x, i in enumerate(input_list)]

Both have no significant difference in computation time. (see comments)

Georgy
  • 12,464
  • 7
  • 65
  • 73
arnaud
  • 3,293
  • 1
  • 10
  • 27
  • 6
    Seems overkill when you could just do `a = [i**x for x, i in enumerate([5, 10, 20])] ` – roganjosh Feb 05 '18 at 21:38
  • Sure, your solution works too and is more pythonic :-). No idea about speed though – arnaud Feb 05 '18 at 21:40
  • 1
    Sorry, first timings were incorrect. But the corrected timings show no difference. With a list of 1000 values: mine: `7.34 ms ± 70.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)` and yours: `7.38 ms ± 9.59 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)` – roganjosh Feb 05 '18 at 21:59
  • Thanks for running that. I guess it's a matter of style then :-) – arnaud Feb 06 '18 at 00:06
  • 1
    No, I meant what I put. Your change to my code does not give the correct answer. – roganjosh Feb 06 '18 at 00:25
0

a very simple way is use a loop like following

for i in range(0,len(input)):
print(input[i]**i)
Sir1
  • 732
  • 1
  • 8
  • 18
0

Your own solution is neither correct nor pythonic . Here is your code will fail :

So if my input is :

3 2 5 3 2 5

output should be :

[1, 2, 25, 27, 16, 3125]

But your code will give :

[1, 2, 25, 1, 2, 25]

Because after 3,2,5 when again 3 comes in the list then index(i) will choose first 3 in the list not second 3 or third or any. So index will always choose first appear numbers and hence you will get the wrong output.

The right way is use enumerate :

print([j**i for i,j in enumerate([int(i) for i in input().split()])])

output:

[1, 2, 25, 27, 16, 3125]
Community
  • 1
  • 1
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88
0

You need to split the input into a list, then use enumerate to get the index position and the value. Since the value will be a string, use int to convert and then raise it to the power.

x = input('Enter the list of numbers : ')
y = [int(i) for i in x.split(',')]
print ([int(a)**i for i,a in enumerate(y)])

Code output will be:

Enter the list of numbers : 3,2,5
[1, 2, 25]
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
-1

I actually figured it out myself! I'm sure you are all very proud. The below code will produce the output I'm looking for. Thank you though!

firstlist = [int(x) for x in input().split()]
newlist = [i ** (int(firstlist.index(i))) for i in firstlist]
  • You don't really need to lookup for finding the right indices as you know they'll come sorted... thus the use of `enumerate` suggested by @roganjosh below – arnaud Feb 05 '18 at 21:41
  • 7
    I think this will give the wrong answer for `[1, 2, 2]`. – Robᵩ Feb 05 '18 at 21:41