Python for-in循环前面有一个变量 [英] Python for-in loop preceded by a variable

查看:1195
本文介绍了Python for-in循环前面有一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  foo = [x for x in bar if x.occupants> 1] 

在Google上搜索并搜索后,找不到这个功能。也许我不是在寻找正确的东西,而是在这里。任何输入在揭穿这个速记非常赞赏。

解决方案

目前的答案是好的,但不要谈论他们是如何语法糖到某种我们习惯的模式。



让我们从一个例子开始,假设我们有10个数字,并且我们想要一个大于5的子集。

 >>>数字= [12,34,1,4,4,67,37,9,0,81] 



<对于上面的任务,下面的方法是完全相同的,并且从最详细到简洁,可读和 pythonic

方法1

  result = [] 
用于范围内的索引(len(数字)):
如果数字[index]> 5:
result.append(numbers [index])
打印结果#Prints [12,34,67,37,9,81]

    

结果= []
数字中的数字:
if number> 5:
result.append(number)
print result #Prints [12,34,67,37,9,81]



方法3(输入列表理解)



  result =数字如果数字> 5] 



或更一般地说:



 [函数(数字)为数字,如果条件(数字)] 



其中:

$ ul
  • 函数(x)需要一个 x 并将其转换为有用的(例如: x * x

  • if condition(x)返回任何False-y值(False,None,空字符串,空列表等等),那么当前的迭代将被跳过(认为继续)。如果该函数返回一个非False-y值,那么当前值将它传递给最终的结果数组(并经过上面的转换步骤)。


    要理解语法稍有不同,请查看下面的Bonus部分。



    有关更多信息,请按照教程链接所有其他答案: 列表理解



    < hr>

    奖金



    (稍微不合pythonic,但为了完整性而将其放在此处)

    上面的例子可以写成:

    $ $ $ $ $ $ c $ result = filter(lambda x:x> ; 5,数字)

  • 上面的一般表达式可以写成:

      result = map(function,filter(condition,numbers))#result是Py2中的一个列表
    pre>

    foo = [x for x in bar if x.occupants > 1]
    

    After googling and searching on here, couldn't figure out what this does. Maybe I wasn't searching the right stuff but here it is. Any input in debunking this shorthand is greatly appreciated.

    解决方案

    The current answers are good, but do not talk about how they are just syntactic sugar to some pattern that we are so used to.

    Let's start with an example, say we have 10 numbers, and we want a subset of those that are greater than, say, 5.

    >>> numbers = [12, 34, 1, 4, 4, 67, 37, 9, 0, 81]
    

    For the above task, the below approaches below are totally identical to one another, and go from most verbose to concise, readable and pythonic:

    Approach 1

    result = []
    for index in range(len(numbers)):
        if numbers[index] > 5:
            result.append(numbers[index])
    print result  #Prints [12, 34, 67, 37, 9, 81]
    

    Approach 2 (Slightly cleaner, for-in loops)

    result = []
    for number in numbers:
        if number > 5:
            result.append(number)
    print result  #Prints [12, 34, 67, 37, 9, 81]
    

    Approach 3 (Enter List Comprehension)

    result = [number for number in numbers if number > 5]
    

    or more generally:

    [function(number) for number in numbers if condition(number)]
    

    where:

    • function(x) takes an x and transforms it into something useful (like for instance: x*x)
    • if condition(x) returns any False-y value (False, None, empty string, empty list, etc ..) then the current iteration will be skipped (think continue). If the function return a non-False-y value then the current value makes it to the final resultant array (and goes through the transformation step above).

    To understand the syntax in a slightly different manner, look at the Bonus section below.

    For further information, follow the tutorial all other answers have linked: List Comprehension


    Bonus

    (Slightly un-pythonic, but putting it here for sake of completeness)

    The example above can be written as:

    result = filter(lambda x: x > 5, numbers)
    

    The general expression above can be written as:

    result = map(function, filter(condition, numbers)) #result is a list in Py2
    

    这篇关于Python for-in循环前面有一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆