是否有列表理解? [英] Is there a list comprehension for this?

查看:76
本文介绍了是否有列表理解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定:

dw = [1,-1.1,+ 1.2]

列表''w''定义为


w [0] = dw [0],

w [1] = w [0] + dw [ 1],

w [2] = w [1] + dw [2]


是否有列表理解或地图表达式在一个或2

行。

解决方案

dw = [1,-1.1,+ 2]


>

假设我要创建一个列表''w'',定义为


w [0] = dw [0],

w [1] = w [0] + dw [1],

w [2] = w [1] + dw [2]


是否有列表推导或地图表达式在一行或两行
行中执行。



好​​吧,虽然它不是非常有效,但你可以做一些像


w = [sum(dw [ :i))for x in xrange(1,len(dw)+1)]


或者,如果你需要更大的len(dw)值的效率,你

可以做类似


>> def f(x) :



.... i = 0

....对于x中的项目: br />
.... i + = item

....收益率我

....


>> list(i for f in f(dw))



[1,-0.10000000000000009,1.0999999999999999]


只是一些想法,


- tkc


liam_herron写道:

鉴于:

dw = [1,-1.1,+ 1]


假设我要创建一个列表''w''定义为


w [0] = dw [0],

w [1] = w [0] + dw [ 1],

w [2] = w [1] + dw [2]


是否有列表理解或地图表达式在一个或2

行。



这是一个你想知道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)


liam_herron写道:


鉴于:

dw = [1,-1.1,+ 1.2]


假设我要创建一个定义为
的列表''w' br />
w [0] = dw [0],

w [1] = w [0] + dw [1],

w [2] = w [1] + dw [2]


是否有列表推导或地图表达式在一行或两行
行中执行。



一种方法是使用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.

解决方案

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

>>def f(x):

.... i = 0
.... for item in x:
.... i += item
.... yield i
....

>>list(i for i in f(dw))

[1, -0.10000000000000009, 1.0999999999999999]

Just a few ideas,

-tkc


liam_herron wrote:

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.


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)


liam_herron wrote:

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.

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


这篇关于是否有列表理解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆