了解涉及最小功能的列表理解

发布于 2025-01-28 03:19:30 字数 212 浏览 1 评论 0原文

我对list理解非常精通。

但是,我无法理解以下语句:

min((-len(list), list[-1] - list[0] + 1) for list in [[0, 4], [1, 2]])[1]

我只了解[[0,4],[1,2]] 中的列表的部分,其中我们正在迭代列表。

I am well-versed with list comprehension.

However, I am not able to understand the below statement:

min((-len(list), list[-1] - list[0] + 1) for list in [[0, 4], [1, 2]])[1]

I only understand the portion for list in [[0, 4], [1, 2]] wherein we are iterating the list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

谜泪 2025-02-04 03:19:30

因此,<代码>列表将依次是[0,4][1,2]。对于第一个,元组将为(-2,5)(5 == 4-0 + 1),在第二个,元组将为(-2) ,2)(2 == 2-1 + 1)。其中最小的是(-2,2),所以这就是返回的。

因此,这试图找到最长的订订符,并将返回第一个元素和大小列表的最后一个元素之间的最小差异。

So, list is going to be, in turn, [0,4], and [1,2]. For the first, the tuple is going to be (-2, 5) (5 == 4 - 0 + 1), for the second, the tuple is going to be (-2, 2) (2 == 2 - 1 + 1). The smallest of those is (-2,2), so that's what will be returned.

So, this is trying to find the longest sublists, and will return the smallest difference between the first element and the last element of the lists that size.

再浓的妆也掩不了殇 2025-02-04 03:19:30

它等同于以下代码段:

min((-len(list), list[-1] - list[0] + 1) for list in [[0, 4], [1, 2]])[1]
newlist = list()
for list in [[0, 4], [1, 2]]:
    newlist.append((-len(list), list[-1] - list[0] + 1))
min = min(newlist)
output = min[1]

It's equivalent to the following code snippet:

min((-len(list), list[-1] - list[0] + 1) for list in [[0, 4], [1, 2]])[1]
newlist = list()
for list in [[0, 4], [1, 2]]:
    newlist.append((-len(list), list[-1] - list[0] + 1))
min = min(newlist)
output = min[1]

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