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

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

问题描述

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

在谷歌上搜索并在这里搜索后,无法弄清楚这是做什么的.也许我没有搜索正确的东西,但它在这里.非常感谢您对揭穿此简写的任何意见.

解决方案

目前的答案很好,但不要谈论它们如何只是 语法糖到一些我们习惯的模式.

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

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

对于上述任务,以下方法彼此完全相同,从最冗长到简洁、可读和pythonic:

方法一

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

方法 2(更简洁的 for-in 循环)

result = []对于数字中的数字:如果数量>5:结果.追加(数字)打印结果 #Prints [12, 34, 67, 37, 9, 81]

方法 3(输入列表理解)

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

或更一般地说:

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

哪里:

  • function(x) 接受一个 x 并将其转换为有用的东西(例如:x*x)
  • 如果 condition(x) 返回任何 False-y 值(False、None、空字符串、空列表等),那么当前迭代将被跳过(想想 continue).如果该函数返回一个非 False-y 值,则当前值会使其成为最终结果数组(并通过上面的转换步骤).

要以稍微不同的方式理解语法,请查看下面的奖励部分.

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

<小时>

奖金

(有点非 Pythonic,但为了完整起见把它放在这里)

上面的例子可以写成:

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

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

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

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天全站免登陆