English 中文(简体)
替换时改为清单理解
原标题:replacing while loop with list comprehension

It is common to express for loops as list comprehensions:

mylist=[]
for i in range(30):
    mylist.append(i**2)

这相当于:

mylist = [i**2 for i in range(30)]

是否存在一种机制,通过这种机制可以同时进行循环?

mylist=[]
i=0
while i<30:
    mylist.append(i**2)
    i+=1

当然,从这个简单的例子来看,很容易将“<>条码/条码>格式”改为“()”,然后改为“理解”清单,但“如果它如此容易”?

e.g.

mylist = [i**2 while i=0;i<30;i++ ]

(当然,上述伪装编码为合法假冒)(itertools) 想到这种事情,但我不知道这一模块是可怕的。

http://www.ohchr.org。

An (very simple) example where I think a while comprehension would be useful would be:

dt=0.05
t=0
mytimes=[]
while t<maxtime:
   mytimes.append(t)
   t+=dt

这可转化为:

dt=0.05
t=0
nsteps=maxtime/dt
mytimes=[]
for t in (i*dt for i in xrange(nsteps)):
    mytimes.append(t)

可作为(详细)清单书写:

nsteps=maxtime/dt
mytimes=[t for t in (i*dt for i in xrange(nsteps)] 

但我想,虽然休息时间是MUCH更容易读(没有指数错误) 另外,如果您的反对(dt)支持+,但不是*? 如果<代码>最大时间,就会出现更为复杂的事例。 a. 每一轮休息时间的变化......

最佳回答

如果在座右铭检查一个正在加固的地方变量,那么你就应当将其改为旁观或相应名单。

只有当你能够而不是<>/strong>表示 lo时,你才能使用 lo。 典型使用案例的一个例子是:Event, 或低水平的 lo,呼吁加入本地法典。 因此,(正确使用)休息时间很少,最好写出。 “虽然理解,但更难以阅读。

如果你只是想恢复多种价值观,你应考虑撰写一份

例如,您编辑的算法应作为(http://docs.scipy.org/doc/numpy/vis/generated/numpy.arange.html#numpy-arange>numpy.arange<):

mytimes = numpy.arange(0, maxtime, 0.05)

或者,与发电机一起:

def calcTimes(maxtime):
  dt = 0.05
  t = 0
  while t < maxtime:
   yield t
   t += dt
问题回答

暂无回答




相关问题
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 ]="...