English 中文(简体)
Python 转换定序的绝对变化相对变化列表
原标题:Python Convert ordered list of relative changes to absolute changes
  • 时间:2012-05-22 19:42:26
  •  标签:
  • python

假设我有一套目标 包含:(相对变化,改变的时间)

(+1,0) (-1, 1) (+1,3) (+1, 3) (-1, 5) (+1, 9)  

现在我想用其绝对值来取代相对变化, 从0开始:

(1,0) (0, 1) (1,3) (2, 3) (1, 5) (2, 9)
 0+1   0+1-1  0+1-1+1 ...

这样做的最佳方法是什么?是否有Python功能允许我

  • Iterate over a (ordered) list of objects
  • Internally store an absolute value
  • read the change from each object, update the internal absolute value
  • replace the relative change with the absolute value
最佳回答

以下是您如何用列表理解来做到这一点:

>>> data = [(+1,0), (-1, 1), (+1,3), (+1, 3), (-1, 5), (+1, 9)]
>>> [(sum(x[0] for x in data[:i+1]), data[i][1]) for i in range(len(data))]
[(1, 0), (0, 1), (1, 3), (2, 3), (1, 5), (2, 9)]

或效率略高一点( 调用 < code> sum () 来计算每个值) :

result = [data[0]]
for change, t in data[1:]:
    result.append((result[-1][0]+change, t))

既然你说这些是对象,你可能需要用属性取而代之的索引,例如 x[0] in sum(x[0])中的 in data[:i+1]]中的 x 可能变成 x.change

问题回答

能够 以列表理解方式这样做, 但从您请求的具体条件来看, 这对我来说更像是生成器的工作。 这是一个非常普遍的解决方案, 应该同时使用序列和迭代器。 它在传递给它的可使用性上, 运行相当于 Haskell s < code> scanl 的函数, 其初始值作为最后的参数, 可选的初始值 。

第一个参数应该是一个函数,它需要两个参数 -- -- 当前累积状态和序列中的下一个项 -- -- 并返回下一个累积状态。它可能像 operator.add 或更复杂的东西一样简单。

>>> def scan(f, seq, init=None):
...     seq = iter(seq)
...     state = seq.next() if init is None else init
...     yield state
...     for i in seq:
...         state = f(state, i)
...         yield state

累计总和(即三角数字):

>>> import operator
>>> list(scan(operator.add, range(10)))
[0, 1, 3, 6, 10, 15, 21, 28, 36, 45]

以不同的初始值开始 :

>>> list(scan(operator.add, range(1, 10), 10))
[10, 11, 13, 16, 20, 25, 31, 38, 46, 55]

适用于您的问题 :

>>> diffs = [(1, 0), (-1, 1), (1, 3), (1, 3), (-1, 5), (1, 9)]
>>> list(scan(lambda x, y: (x[0] + y[0], y[1]), diffs))
[(1, 0), (0, 1), (1, 3), (2, 3), (1, 5), (2, 9)]

最初价值不同 只是为了取乐

>>> list(scan(lambda x, y: (x[0] + y[0], y[1]), diffs, (5, -1)))
[(5, -1), (6, 0), (5, 1), (6, 3), (7, 3), (6, 5), (7, 9)]




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...