for-if without list comprehension in one line

2019-03-27 15:12发布

问题:

can this be written in one line without List Comprehensions?

for x in vec: 
    if x > 3:
         ...
         ...

回答1:

No, you can't. The Python language reference states:

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)* [";"]

Note that when for is followed by a statement without an indentation, that statement must be a stmt_list, which only allows simple_stmt instances. simple_stmt is this:

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

Which doesn't include compound statements like if and for.


That said, keep in mind that Python's syntax is aimed at clarity. Therefore it's better not to nest such statements, this is what generators/list comprehensions were made for. If you deem your computation to be simple enough for a single line, then comprehensions are for you. Otherwise, you really don't want to clutter the code by having everything on a single line - break it up nicely with indentation. A few extra lines don't cost much these days.



回答2:

See @KennyTM... no reason to compress that much.

What being said, for x in (i in vec if i > 3) does the job, as well as itertools.ifilter (or just the builtin filter in Python 3) with a lambda x: x > 3 predicate. They work with all iterables as well, and are lazy (e.g. if you break during the loop, you didn't check a single item too much).



回答3:

It can, but list comprehensions/generator expressions are the exact sort of thing that should be used here. Depending on what you want to do in your if block, you could use some form of map or reduce, but list comprehensions and generator expressions are likely the best way to do it.



回答4:

Yes

for x in filter(lambda i:i>3,vec):



回答5:

You can imagine somethings like this :

def do_something(value):
    ...

def do_otherthing(value):
    ...


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