English 中文(简体)
如何写发电机,在下午[闭门]列出理解
原标题:How to write Generators , list comprehension in python [closed]
最佳回答

对我来说,一些例子发挥了作用。 在你利用清单解决问题之前,清单核对表可能不会自然发生,或者说,如果在<<>代码>上填写<>代码/代码>,则通常不会解决。

某些想法(其他可能有更好的实例):

  • Given a list of words, find the words of length 5.
  • Given a list of numbers, return the subset of primes, multiplied by 2
  • Given a list of fortune cookie sayings (or tweets), return a list of the sayings that are under 10 words long with in bed added to the end of each.
问题回答

在这方面,我是如何通过名单理解来思考的。

1) I need to output a list

(2) Im, 先从清单/备份。

3) 我要么需要就所有要素采取行动,要么选择原始清单中的具体内容。

这导致我进行以下施工:

<代码>output = 选择(投入)]中的x[千兆(x)]

男性是改变一个要素的一些功能。 例如,我可以使用<代码>x.down(>)来降低要素。

我总是使用<条码>x作为代号。 只是保持一切一致(我从来不把它当作“<>条/代码>的序号”的传导器。

选择权()是产出<代码>True或<代码>False的职能。 如果说的话,通常会采取某种方式。 我主要利用这一试验进行生存,特别是如果I mmangling产出。 例如,如果输入,则输入中的x<代码>。

清单核对表确实是巨大的。 我认为,它们确实提高了可读性,而且其方式超过了一种 ne。 但是,他们记得,他们只是一线 lo。

尝试为假肢撰写文件并试图将其转化为理解清单,可能最容易。

如果你刚刚从清单中抽出,那么我怎么走。 第一封信给你正常的住宿:

results = []

# get all keys that aren t test strings and add to results list
for a in blah.keys():
  if a not in ( test ,  foo ):
    results.append(a)

现在要制定一份清单,让人们了解情况,如果发言顺序相同,并放在同一行文上,最终结果代码就在开头:

results = [ results.append(a) for a in blah.keys() if a not in ( test ,  foo ) ]

我们几乎在那里! 我们不需要人工阅读(a),因为我们在该部分的表述中将自动附在名单之后,因此最后的表述是:

 # get all keys that aren t test strings and add to results list
 results = [ a for a in blah.keys() if a not in ( test ,  foo ) ]

HTH!

清单核对表的概念与关于住所的思路相同,但只是按一线计算,你可以将其在线用作理由。

myFunc([s.strip() for s in mystrings])

Generator comprensions dont create the list right away in memory. They step through the values when you iterate over them and can be more efficient if you will stop looping over them early and dont need the entire result right away





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

热门标签