2

Learning python I encountered the following problem. I already have some list with integer numbers (for example initial_list). Then I'm trying to create list (for example result) with following numbers pattern:

result[len(result) - 1] = 1
result[i] = result[i + 1] * initial_list[i]

Example:

initial_list = [2, 2, 2, 2, 3, 2, 2]
result = [192, 96, 48, 24, 12, 4, 2, 1]

Here is my first implementation:

import numpy as np
initial_list = [2, 2, 2, 2, 3, 2, 2]
result = [1]
for number in reversed(initial_list):
    result.append(result[-1] * number)
result = np.array(result[::-1])

Here is my second implementation:

import numpy as np
initial_list = [2, 2, 2, 2, 3, 2, 2]
result = np.ones(len(initial_list) + 1)
for i, number in enumerate(reversed(initial_list)):
    result[len(result) - i - 2] = result[len(result) - i - 1] * number

I guess second one is much better because it does not contain append method and initialize list with concrete size. Also result = np.ones(len(initial_list) + 1) consists with float point number which is not correct. I wonder whether there is a simpler version of the implementation of this algorithm.

After that algorithm I'm creating OrderedDict:

from collections import OrderedDict
ordered_dict = OrderedDict(zip(other_list, result))

Maybe I can reverse result list during creating OrderedDict for O(1) "on fly" like reversed works in for looping, so that I can simplify algorithm above.

8
  • what is the algorithm supposed to do? Commented Jun 28, 2015 at 22:16
  • @PadraicCunningham It should create list from initial list with patter that mentioned above:result[len(result) - 1] = 1, result[i] = result[i + 1] * initial_list[i] Commented Jun 28, 2015 at 22:18
  • I think a list comprehension would be a good idea, but I'm having trouble figuring out your algorithm. For instance, result[len(result) - 1] = 1 only makes sense if result has some data in it. What does result look like at the beginning? Commented Jun 28, 2015 at 22:21
  • 1
    @Quill Maybe I misunderstood? I read the "I wonder whether there is a simpler version of the implementation of this algorithm." part specifically and thought that it would make more sense there since it seems to be a problem of code optimization. In the on-topic section, it seems to say that "Best practices and design pattern usage" are on-topic "for feedback on a specific working piece of code from your project". Clearly, you would know better than me what is off-topic, but I just don't understand why you believe so. Commented Jun 28, 2015 at 23:30
  • 2
    You'd be correct, it seems, thank for you reading the on-topic section, and help center guidelines. OP, feel free to take this to Code Review if you please. Commented Jun 28, 2015 at 23:33

3 Answers 3

4

If numpy is an option, you can do it with cumprod:

import numpy as np

initial_list = [2, 2, 2, 2, 3, 2, 2]

>> np.append(np.cumprod(initial_list[:: -1])[:: -1], [1])
array([192,  96,  48,  24,  12,   4,   2,   1])
Sign up to request clarification or add additional context in comments.

3 Comments

numpy is definitely an option, they are using it ;)
@PadraicCunningham :-) Right you are. Thanks!
@AmiTavory In a single line, that is what I need. Thanks!
3

You can use itertools.accumulate and operator.mul

>>> from itertools import accumulate
>>> from operator import mul
>>> a = [2, 2, 2, 2, 3, 2, 2]
>>> list(accumulate(reversed(a + [1]), mul))[::-1]
[192, 96, 48, 24, 12, 4, 2, 1]

1 Comment

Nice combination of itertools + operator.
0

Why can't you just do

result = [0] * len(initial_list)
result[-1] = 1
for i in xrange(len(result) - 2, -1, -1):
  result[i] = result[i + 1] * initial_list[i]

Comments

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.