0

I need to calculate the value an item in a log() list based on how much the item added to the list. Example, I have a list like this (using integers, not floats, for simplicity sake);

>>> import math
>>> [int(math.log(1 + n) * 4) for n in range(1, 10)]
[2, 4, 5, 6, 7, 7, 8, 8, 9]

I want a list like [2, 2, 1, 1, 1, 0, 1, 0, 1], ie, it needs to look at the previous element in the list to get the values.

This is going to be used almost like the example, but with a much bigger list. So the ultimate solution would a mathematical way of doing this, not creating a python-function to do the job.

xeor
  • 5,301
  • 5
  • 36
  • 59
  • 1
    Not sure I understand what the problem with your approach. – amit Apr 22 '15 at 11:19
  • I'm not sure what you want, the `I want a list like [2, 2, 1, 1, 0, 0, 1, 0, 1], ie, it needs to look at the previous element in the list to get the values` part confuses me. – Morb Apr 22 '15 at 11:19
  • If you want a `mathematical` way, I'll flag it as offtopic and redirect you to correct StackExchange site. If you want a Python code, we'll need something more precise than `it needs to look at the previous element in the list to get the values`. Actual expected value for `f(range(1,10))` is `[2, 2, 1, 1, 0, 0, 1, 0, 1]`?. If yes, how do you came up with it? If it's discreet differential I think it's undefined for first element and expected count on output would be 8? (At least that's what Matlab/numpy does). – Łukasz Rogalski Apr 22 '15 at 11:19
  • Not sure what constitutes mathematical vs python but you mean like `[int(math.log(1 + n) * 4) - int(math.log(n) * 4) for n in range(1, 10)]`? – zehnpaard Apr 22 '15 at 11:20
  • 2
    Do you mean you want the *differences* between the items in the list? That seems unrelated to `log`, but have a look at e.g. http://stackoverflow.com/q/5434891/3001761 – jonrsharpe Apr 22 '15 at 11:22
  • 1
    I'm guessing your example output is incorrect, probably should be `[2, 2, 1, 1, 1, 0, 1, 0, 1]` – zehnpaard Apr 22 '15 at 11:23
  • So a differential from `[12345678, 123456789, 123456790]` should be `[12345678, 1, 1]`? I'm a little troubled with that, does not seem very elegant. – Łukasz Rogalski Apr 22 '15 at 11:43
  • I need to implement this in python, and I need to use log(). I was including log() in the example if to cover both answers using a mathematical solution or a python one. I will ask the question on the math-stackexchange. – xeor Apr 22 '15 at 12:42

2 Answers2

2

It looks like you might want the differences between the items in the list.

A simple way to do it with python is

[int(math.log(1 + n) * 4) - int(math.log(n) * 4) for n in range(1, 10)]

Since you said "(using integers, not floats, for simplicity sake)", I assume the actual problem does not include the int part? In that case you can simplify mathematically like this:

[4 * (math.log(1 + n) - math.log(n)) for n in range(1, 10)]

[4 * math.log((1 + n) / n) for n in range(1, 10)]

by properties of logarithms.

camz
  • 605
  • 3
  • 15
0

Try the following:

In [1]: items = [2, 4, 5, 6, 7, 7, 8, 8, 9]

In [2]: items[:1] + [j - i for i, j in zip(items, items[1:])]
Out[2]: [2, 2, 1, 1, 1, 0, 1, 0, 1]

In [3]: 
Paddy3118
  • 4,704
  • 27
  • 38