带有内联的If-else的元组(UN)的优先级

发布于 2025-02-12 12:05:46 字数 916 浏览 1 评论 0原文

事先为晦涩的头衔表示歉意。我不确定如何用我遇到的内容。

想象一下,您在变量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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白馒头 2025-02-19 12:05:46
title, author = title_author.split("-", 1) if title_author else None, None

与:

title, author = (title_author.split("-", 1) if title_author else None), None

因此,作者始终是 none


解释

来自官方doc

分配语句评估表达式列表(请记住
这可以是单个表达式或逗号分隔列表,后者
产生元组),并将单个结果对象分配给每个对象
目标列表,从左到右。

也就是说,Interupter将寻找(x,y)=(a,b),并分配值为x = ay = b < /代码>。

在您的情况下,有两个解释,主要不同的是:

  1. title,rution =(title_author.split(“ - ”,1)如果title_author else quthor none none),none
    正在为两个变量分配两个值(列表或无和无),并且不需要解压缩。

  2. title,作者= title_author.split(“ - ”,1)如果title_author else(none,none,none)实际上将一个值(列表或元组)分配给两个变量,需要一个拆卸步骤将两个变量映射到列表/元组中的两个值。

由于可以完成选项1,而无需解开包装,即较少的操作,因此,无明确说明将使用选项1。

title, author = title_author.split("-", 1) if title_author else None, None

is the same as:

title, author = (title_author.split("-", 1) if title_author else None), None

Therefore, author is always None


Explaination:

From official doc

An assignment statement evaluates the expression list (remember that
this can be a single expression or a comma-separated list, the latter
yielding a tuple) and assigns the single resulting object to each of
the target lists, from left to right.

That is to say, the interrupter will look for (x,y)=(a,b) and assign value as x=a and y=b.

In your case, there are two interpretation, the main differece is that :

  1. 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.

  2. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文