问题描述
给定:dw = [1,-1.1,+ 1.2]
列表''w''定义为
w [0] = dw [0],
w [1] = w [0] + dw [ 1],
w [2] = w [1] + dw [2]
是否有列表理解或地图表达式在一个或2
行。
好吧,虽然它不是非常有效,但你可以做一些像
w = [sum(dw [ :i))for x in xrange(1,len(dw)+1)]
或者,如果你需要更大的len(dw)值的效率,你
可以做类似
.... i = 0
....对于x中的项目: br />
.... i + = item
....收益率我
....
[1,-0.10000000000000009,1.0999999999999999]
只是一些想法,
- tkc
这是一个你想知道aw [n]的函数吗?
然后:
def w(n):
返还金额((1 + i * 0.1)*(i%2和1或-1)for x in xrange(n))
否则:
n,w,x = 3,[],0
for y in((1 + i * 0.1)*(i%2和1或-1)for x in xrange(n)):
x + = y
w.append(x)
一种方法是使用numpy(numpy.scipy.org):
在[40]中:来自numpy import cumsum
在[41]中:dw = [1,-1.1,+ 1.2]
在[42]中:cumsum(dw)
Out [42]:array([1.,-0.1,1.1])
如果你正在进行大量的数值计算,你可能会想要一些
numpy提供的其他东西。
-
Robert Kern
我已经开始相信整个世界都是一个谜,一个无害的谜团
由于我们疯狂地试图解释它而使它变得可怕,好像它已经有了b
一个潜在的事实。
- Umberto Eco
Given:
dw = [ 1, -1.1, +1.2 ]
Suppose I want to create a list ''w'' that is defined as
w[0] = dw[0],
w[1] = w[0] + dw[1],
w[2] = w[1] + dw[2]
Is there a list comprehension or map expression to do it in one or 2
lines.
Well, while it''s not terribly efficient, you can do something like
w = [sum(dw[:i]) for i in xrange(1,len(dw)+1)]
Or, if you need the efficiencies for larger len(dw) values, you
could do something like
.... i = 0
.... for item in x:
.... i += item
.... yield i
....
[1, -0.10000000000000009, 1.0999999999999999]
Just a few ideas,
-tkc
Is this a function where you just want to know a w[n]?
then:
def w(n):
return sum((1 + i * 0.1) * (i % 2 and 1 or -1) for i in xrange(n))
otherwise:
n, w, x = 3, [], 0
for y in ((1 + i * 0.1) * (i % 2 and 1 or -1) for i in xrange(n)):
x += y
w.append(x)
One way is to use numpy (numpy.scipy.org):
In [40]: from numpy import cumsum
In [41]: dw = [1, -1.1, +1.2]
In [42]: cumsum(dw)
Out[42]: array([ 1. , -0.1, 1.1])
If you''re doing a lot of numerical computing, you''ll probably want a number of
other things that numpy provides.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
这篇关于是否有列表理解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!