带有内联的If-else的元组(UN)的优先级
事先为晦涩的头衔表示歉意。我不确定如何用我遇到的内容。
想象一下,您在变量title_author
中,与其作者一起有一本书的标题,由-
隔开。您从网络上删除了这些信息,因此很可能是none
。显然,您想将标题与作者分开,因此您将使用Split。但是,如果title_author
是没有开始的,那么您只希望title
和作者
be none
。
我认为以下是一个很好的方法:
title_author = "In Search of Lost Time - Marcel Proust"
title, author = title_author.split("-", 1) if title_author else None, None
print(title, author)
# ['In Search of Lost Time ', ' Marcel Proust'] None
但是令我惊讶的是,title
现在是拆分的结果,作者
是none
。解决方案是明确表明依然子句是括号的元组。
title, author = title_author.split("-", 1) if title_author else (None, None)
print(title, author)
# In Search of Lost Time Marcel Proust
那为什么会发生呢?在第一种情况下导致结果的执行顺序是什么?
Apologies in advance for the obscure title. I wasn't sure how to phrase what I encountered.
Imagine that you have a title of a book alongside its author, separated by -
, in a variable title_author
. You scraped this information from the web so it might very well be that this item is None
. Obviously you would like to separate the title from the author, so you'd use split. But in case title_author
is None to begin with, you just want both title
and author
to be None
.
I figured that the following was a good approach:
title_author = "In Search of Lost Time - Marcel Proust"
title, author = title_author.split("-", 1) if title_author else None, None
print(title, author)
# ['In Search of Lost Time ', ' Marcel Proust'] None
But to my surprise, title
now was the result of the split and author
was None
. The solution is to explicitly indicate that the else clause is a tuple by means of parentheses.
title, author = title_author.split("-", 1) if title_author else (None, None)
print(title, author)
# In Search of Lost Time Marcel Proust
So why is this happening? What is the order of execution here that lead to the result in the first case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与:
因此,
作者
始终是 none解释:
来自官方doc
也就是说,Interupter将寻找
(x,y)=(a,b)
,并分配值为x = a
和y = b < /代码>。
在您的情况下,有两个解释,主要不同的是:
title,rution =(title_author.split(“ - ”,1)如果title_author else quthor none none),none
正在为两个变量分配两个值(列表或无和无),并且不需要解压缩。
title,作者= title_author.split(“ - ”,1)如果title_author else(none,none,none)
实际上将一个值(列表或元组)分配给两个变量,需要一个拆卸步骤将两个变量映射到列表/元组中的两个值。由于可以完成选项1,而无需解开包装,即较少的操作,因此,无明确说明将使用选项1。
is the same as:
Therefore,
author
is alwaysNone
Explaination:
From official doc
That is to say, the interrupter will look for
(x,y)=(a,b)
and assign value asx=a
andy=b
.In your case, there are two interpretation, the main differece is that :
title, author = (title_author.split("-", 1) if title_author else None), None
is assigning two values (a list or a None and a None) to two variables and no unpacking is needed.
title, author = title_author.split("-", 1) if title_author else (None, None)
is actually assigning one value (a list or a tuple) to two variable, which need an unpacking step to map two variables to the two values in the list/tuple.As option 1 can be completed without unpacking, i.e. less operation, the interrupter will go with option 1 without explicit instructions.