pythonic way to rewrite an assignment in an if statement

前端 未结 7 836
隐瞒了意图╮
隐瞒了意图╮ 2020-12-19 10:37

Is there a pythonic preferred way to do this that I would do in C++:


for s in str:
    if r = regex.match(s):
        print r.groups()

I r

7条回答
  •  一个人的身影
    2020-12-19 11:22

    How about

    for r in [regex.match(s) for s in str]:
        if r:
            print r.groups()
    

    or a bit more functional

    for r in filter(None, map(regex.match, str)):
        print r.groups()
    

提交回复
热议问题