结合 for 循环和 if 语句的 Pythonic 方式
我知道如何在单独的行上使用 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 应该工作的方式吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
如果生成器表达式变得过于复杂或复杂,您也可以使用 生成器:
You can use generators too, if generator expressions become too involved or complex:
基于此处的文章:https://towardsdatascience.com/a-compressive-hands-on-guide-to-transfer-learning-with-real-world-applications-in-deep-learning-212bf3b2f27a
出于同样的原因,我使用了以下代码,并且运行得很好:
这一行是程序的一部分!这意味着 XYZ 是一个要预先定义和分配的数组,也是变量 a
使用生成器表达式(在所选答案中推荐)会产生一些困难,因为结果不是一个数组
based on the article here: https://towardsdatascience.com/a-comprehensive-hands-on-guide-to-transfer-learning-with-real-world-applications-in-deep-learning-212bf3b2f27a
I used the following code for the same reason and it worked just fine:
This line is a part of the program! this means that XYZ is an array which is to be defined and assigned previously, and also the variable a
Using generator expressions (which is recommended in the selected answer) makes some difficulties because the result is not an array
使用
intersection
或intersection_update
intersection :
<前><代码>a = [2,3,4,5,6,7,8,9,0]
xyz = [0,12,4,6,242,7,9]
ans = 排序(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 = 集合(a)
b.intersection_update(xyz)
那么
b
就是你的答案Use
intersection
orintersection_update
intersection :
intersection_update:
then
b
is your answer嗯,只需一行即可完成。
This gives you:
Well, it's possible to do it in just one line.
This gives you:
虽然我真的很喜欢已接受的答案,但我想补充
一下讨论。
“优势”是有争议的,但正如这里已经指出的,并引用自 The Zen of Python,“简单胜于复杂”和“可读性很重要”。我声称
continue
的概念没有生成器表达式那么复杂。Although I really like the accepted answer, I would like to add
to the discussion.
The "advantage" is debatable, but as already stated here and cited from the The Zen of Python, "simple is better than complex" and "readability counts". I claim that the concept of
continue
is less complex than generator expressions.查找列表 a 和 b 的唯一公共元素的简单方法:
A simple way to find unique common elements of lists a and b:
您可以使用 生成器表达式,如下所示:
You can use generator expressions like this:
根据The Zen of Python(如果你想知道你的代码是否是“Pythonic”,那就是去的地方):
获取
sorted
的 Pythonic 方式两个
href="http://www.python.org/doc//current/library/stdtypes.html#set">
set
是:或者那些
元素xyz
但不在a
中:但是对于更复杂的循环,您可能希望通过迭代命名良好的 生成器表达式 和/或调用一个命名良好的函数。试图将所有内容都放在一行中很少是“Pythonic”。
更新以下关于您的问题和接受的答案的附加评论
我不确定您想用
enumerate
,但如果a
是字典,您可能需要使用键,如下所示:As per The Zen of Python (if you are wondering whether your code is "Pythonic", that's the place to go):
The Pythonic way of getting the
sorted
intersection
of twoset
s is:Or those elements that are
xyz
but not ina
:But for a more complicated loop you may want to flatten it by iterating over a well-named generator expression and/or calling out to a well-named function. Trying to fit everything on one line is rarely "Pythonic".
Update following additional comments on your question and the accepted answer
I'm not sure what you are trying to do with
enumerate
, but ifa
is a dictionary, you probably want to use the keys, like this:以下是已接受答案的简化/一行:
请注意,
生成器
保持内联。这是在python2.7
和python3.6
上进行了测试(注意print
中的括号;))即使如此,老实说还是很麻烦:
x
被提及四次。The following is a simplification/one liner from the accepted answer:
Notice that the
generator
was kept inline. This was tested onpython2.7
andpython3.6
(notice the parens in theprint
;) )It is honestly cumbersome even so: the
x
is mentioned four times.我个人认为这是最漂亮的版本:
编辑
如果您非常热衷于避免使用 lambda,您可以使用部分函数应用程序并使用运算符模块(提供大多数运算符的功能)。
https://docs.python.org/2/library/operator.html #模块运算符
I personally think this is the prettiest version:
Edit
if you are very keen on avoiding to use lambda you can use partial function application and use the operator module (that provides functions of most operators).
https://docs.python.org/2/library/operator.html#module-operator
我可能会使用:
I would probably use:
我喜欢 Alex 的回答,因为filter 正是应用于列表的 if,因此,如果您想在给定条件下探索列表的子集,这似乎是最有效的自然地,
此方法对于关注点分离很有用,如果条件函数发生变化,唯一需要修改的代码就是函数本身。
当您不需要列表成员时,生成器方法似乎更好,但是对所述成员的修改,这似乎更适合生成器
此外,过滤器与生成器一起工作,尽管在这种情况下效率不高
但是当然,这样写仍然很好这:
I liked Alex's answer, because a filter is exactly an if applied to a list, so if you want to explore a subset of a list given a condition, this seems to be the most natural way
this method is useful for the separation of concerns, if the condition function changes, the only code to fiddle with is the function itself
The generator method seems better when you don't want members of the list, but a modification of said members, which seems more fit to a generator
Also, filters work with generators, although in this case it isn't efficient
But of course, it would still be nice to write like this: