English 中文(简体)
页: 1
原标题:for-if without list comprehension in one line

可以在一行不设名单的情况下书写。

for x in vec: 
    if x > 3:
         ...
         ...
问题回答

www.un.org/Depts/DGACM/index_spanish.htm 页: 1

Compound statements consist of one or more ‘clauses.’ A clause consists of a header and a ‘suite.’ The clause headers of a particular compound statement are all at the same indentation level. Each clause header begins with a uniquely identifying keyword and ends with a colon. A suite is a group of statements controlled by a clause. A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header’s colon, or it can be one or more indented statements on subsequent lines. Only the latter form of suite can contain nested compound statements; the following is illegal, mostly because it wouldn’t be clear to which if clause a following else clause would belong:

if test1: if test2: print x

Indeed, Python generates a SyntaxError for the nested ifs above. More formally regarding for, this is its grammar in Python:

for_stmt ::=  "for" target_list "in" expression_list ":" suite
              ["else" ":" suite]

suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT

stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]

请注意,在<><>t>for>之后,在未作改动的情况下,该声明必须是stmt_list,其中仅允许simple_stmts. www.un.org/Depts/DGACM/index_spanish.htm

simple_stmt ::=  expression_stmt
                 | assert_stmt
                 | assignment_stmt
                 | augmented_assignment_stmt
                 | pass_stmt
                 | del_stmt
                 | print_stmt
                 | return_stmt
                 | yield_stmt
                 | raise_stmt
                 | break_stmt
                 | continue_stmt
                 | import_stmt
                 | global_stmt
                 | exec_stmt

哪一则包含诸如<代码>if和>的复合说明。


尽管如此,我们仍铭记,斯堪的辛奈旨在澄清问题。 因此,最好不要打上这种说法,这就是说什么是发电机/清单。 如果你认为你的计算足够简单,只需一行,那么你就会知道。 否则,你真的不希望通过把一切放在一线上来删除该法典,这完全是同tation。 几天后,几条外线没有花费很多费用。

See @KennyTM... noêt to compress that Much.

说到什么,for x in (i in vec if i > 3) do the work, as well as itertools.ifilter (or only the setin filter in /ecosoc 3) with a lambda x: x > 3migration. 它们也与所有电离层合作,并且是zy(例如,如果你<条码>,)。 在休息期间,你只检查一个物品太多。

它可以,但列出谅解/声明的表述是此处应当使用的确切类型。 根据您在<代码>if栏目中想要做些什么,您可以使用某种<代码>><>>>>>>> 或reduce/code>,但列出理解和生成器的表述可能是这样做的最佳方式。

用于过滤(lambda i:i>3,vec):

你们可以想象这样的东西:

def do_something(value):
    ...

def do_otherthing(value):
    ...


for x in t: do_something(x) if x>3 else do_otherthing(x)




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