简体   繁体   English

此行在此 python 脚本中如何工作

[英]How does this line work in this python script

food = dict(line.split(":", 1) for line in open("file") if line.strip())

I know what this code does but I don't understand why it was put together like this, So can someone explain to me the logic of adding the "if" statement at the end.我知道这段代码做了什么,但我不明白为什么它会这样放在一起,所以有人可以向我解释一下在最后添加“if”语句的逻辑。

How does telling the script to make a dictionary using iteration from a file work, then just adding如何告诉脚本使用文件中的迭代来制作字典,然后添加

if line.strip() 

work?工作? doesn't something need to go after that statement??在该声明之后不需要 go 吗? What is it telling the script since there is no condition after it?它在告诉脚本什么,因为它后面没有条件?

I know this code works because I tried it but I'm baffled at HOW it works.我知道这段代码有效,因为我试过了,但我对它是如何工作的感到困惑。

The if statement is a filter for the generator expression. if 语句是生成器表达式的过滤器。 At the end of a generator expression, you can have an if statement to specify conditions that each item needs to meet to be included in the final generator.在生成器表达式的末尾,您可以有一个 if 语句来指定每个项目需要满足才能包含在最终生成器中的条件。

You might better understand a more simple example:您可能会更好地理解一个更简单的示例:

(i for i in range(100) if i % 3 == 0)

returns a generator that contains every number from 0 to 99 that is divisible by 3.返回一个生成器,其中包含从 0 到 99 的每个可被 3 整除的数字。

In your particular example, the if line.strip() filters the final generator to only strings where line.strip() is True (the idea is probably to make sure that there is some content in each string other than whitespace).在您的特定示例中, if line.strip()将最终生成器过滤为仅line.strip()为 True 的字符串(这个想法可能是为了确保每个字符串中除了空格之外还有一些内容)。

(If you don't know what generators are, see this .) (如果您不知道生成器是什么,请参阅此内容。)

It's a comprehension .是一种理解

Adding a trailing if will check for each element if it is valid whithin your condition and add it to a list (in your case, a generator)添加尾随if将检查每个元素是否在您的条件下有效并将其添加到列表中(在您的情况下为生成器)

>>> [i for i in range(10) if i%2]
[1, 3, 5, 7, 9]

And you got only odd numbers你只有奇数

if line.strip() simply checks that the string is not empty or space-only. if line.strip()只是检查字符串是否为空或只有空格。 Adding the if-statement to the end is simply how the syntax for generator expressions work;将 if 语句添加到末尾只是生成器表达式的语法工作方式; when iterating the lines in the file, the lines where the if-statement is false are excluded.迭代文件中的行时,排除 if 语句为 false 的行。

This uses the list comprehension syntax (or to be more precise, in this case, it's a generator comprehension).这使用列表推导语法(或更准确地说,在这种情况下,它是生成器推导)。 It goes a little like this:它有点像这样:

<expression> for <name> in <iterable>[ if <condition>]

For each item in iterable , it will set name to that item and evaluate expression , but only if condition is truthy.对于iterable中的每个项目,它将为该项目设置name并评估expression ,但前提是condition为真。

So what it does: It iterates over the lines in a file.那么它的作用:它遍历文件中的行。 If the line is empty, it skips it.如果该行为空,则跳过它。 If the line is not empty, it will split it on a colon with a maximum of two items.如果该行不为空,它将在冒号上拆分,最多包含两个项目。 After it has iterated over everything, it will turn it into a dict .在它遍历所有内容之后,它将把它变成一个dict

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM