首页 文章

Pythonic方法结合FOR循环和IF语句

提问于
浏览
207

我知道如何在单独的行上使用for循环和if语句,例如:

>>> a = [2,3,4,5,6,7,8,9,0]
... xyz = [0,12,4,6,242,7,9]
... for x in xyz:
...     if x in a:
...         print(x)
0,4,6,7,9

而且我知道当语句很简单时我可以使用列表推导来组合这些,例如:

print([x for x in xyz if x in a])

但是我找不到的是一个很好的例子(复制和学习)演示了一组复杂的命令(不仅仅是“print x”),这些命令是在for循环和一些if语句的组合之后发生的 . 我期望的东西看起来像:

for x in xyz if x not in a:
    print(x...)

这不是python应该工作的方式吗?

8 回答

  • 3

    我可能会用:

    for x in xyz: 
        if x not in a:
            print x...
    
  • 9

    以下是接受答案的简化/单线程:

    a = [2,3,4,5,6,7,8,9,0]
    xyz = [0,12,4,6,242,7,9]
    
    for x in (x for x in xyz if x not in a):
        print(x)
    
    12
    242
    

    请注意 generator 保持 inline . 这是在 python2.7python3.6 上测试的(注意 print 中的parens;))

  • 2

    我个人认为这是最漂亮的版本:

    a = [2,3,4,5,6,7,8,9,0]
    xyz = [0,12,4,6,242,7,9]
    for x in filter(lambda w: w in a, xyz):
      print x
    

    编辑

    如果您非常希望避免使用lambda,可以使用部分函数应用程序并使用运算符模块(提供大多数运算符的功能) .

    https://docs.python.org/2/library/operator.html#module-operator

    from operator import contains
    from functools import partial
    print(list(filter(partial(contains, a), xyz)))
    
  • 4

    使用 intersectionintersection_update

    • intersection
    a = [2,3,4,5,6,7,8,9,0]
    xyz = [0,12,4,6,242,7,9]
    ans = sorted(set(a).intersection(set(xyz)))
    
    • intersection_update
    a = [2,3,4,5,6,7,8,9,0]
    xyz = [0,12,4,6,242,7,9]
    b = set(a)
    b.intersection_update(xyz)
    

    然后 b 是你的答案

  • 9

    根据The Zen of Python(如果你想知道你的代码是否是"Pythonic",那就是去的地方):

    • 美丽胜过丑陋 .

    • 显式优于隐式 .

    • 简单比复杂更好 .

    • Flat比嵌套更好 .

    • 可读性很重要 .

    得到sorted intersection的两个set的Pythonic方法是:

    >>> sorted(set(a).intersection(xyz))
    [0, 4, 6, 7, 9]
    

    xyz 但_56305_中没有的那些元素:

    >>> sorted(set(xyz).difference(a))
    [12, 242]
    

    但是对于一个更复杂的循环,你可能想通过迭代一个名字很好的generator expression和/或调用一个名字很好的函数来展平它 . 试图将所有东西都放在一条线上很少"Pythonic" .


    更新以下关于您的问题和接受的答案的其他评论

    我不确定你要用_562309做什么,但如果 a 是一个字典,你可能想要使用这些键,如下所示:

    >>> a = {
    ...     2: 'Turtle Doves',
    ...     3: 'French Hens',
    ...     4: 'Colly Birds',
    ...     5: 'Gold Rings',
    ...     6: 'Geese-a-Laying',
    ...     7: 'Swans-a-Swimming',
    ...     8: 'Maids-a-Milking',
    ...     9: 'Ladies Dancing',
    ...     0: 'Camel Books',
    ... }
    >>>
    >>> xyz = [0, 12, 4, 6, 242, 7, 9]
    >>>
    >>> known_things = sorted(set(a.iterkeys()).intersection(xyz))
    >>> unknown_things = sorted(set(xyz).difference(a.iterkeys()))
    >>>
    >>> for thing in known_things:
    ...     print 'I know about', a[thing]
    ...
    I know about Camel Books
    I know about Colly Birds
    I know about Geese-a-Laying
    I know about Swans-a-Swimming
    I know about Ladies Dancing
    >>> print '...but...'
    ...but...
    >>>
    >>> for thing in unknown_things:
    ...     print "I don't know what happened on the {0}th day of Christmas".format(thing)
    ...
    I don't know what happened on the 12th day of Christmas
    I don't know what happened on the 242th day of Christmas
    
  • 249
    a = [2,3,4,5,6,7,8,9,0]
    xyz = [0,12,4,6,242,7,9]  
    set(a) & set(xyz)  
    set([0, 9, 4, 6, 7])
    
  • 29

    您可以像这样使用generator expressions

    gen = (x for x in xyz if x not in a)
    
    for x in gen:
        print x
    
  • 11

    如果生成器表达式过于复杂或复杂,您也可以使用generators

    def gen():
        for x in xyz:
            if x in a:
                yield x
    
    for x in gen():
        print x
    

相关问题