为什么锯齿状数组中第二个索引的负值在 Python 中不起作用?

发布于 2024-09-01 06:58:49 字数 798 浏览 4 评论 0原文

例如,如果我有以下内容(来自 Project Euler 的数据):

s = [[75],
     [95, 64],
     [17, 47, 82],
     [18, 35, 87, 10],
     [20, 4, 82, 47, 65],
     [19, 1, 23, 75, 3, 34],
     [88, 2, 77, 73, 7, 63, 67],
     [99, 65, 4, 28, 6, 16, 70, 92],
     [41, 41, 26, 56, 83, 40, 80, 70, 33],
     [41, 48, 72, 33, 47, 32, 37, 16, 94, 29],
     [53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14],
     [70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57],
     [91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48],
     [63, 66, 4, 68,89, 53, 67, 30, 73, 16, 69, 87, 40, 31],
     [4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23]]

为什么 s[1:][:-1] 给我的结果与 s[1:] 相同code> 而不是(我想要的)[s[i][:-1] for i in range(1,len(s))]。换句话说,为什么Python会忽略我的第二个索引?

For example, if I have the following (data from Project Euler):

s = [[75],
     [95, 64],
     [17, 47, 82],
     [18, 35, 87, 10],
     [20, 4, 82, 47, 65],
     [19, 1, 23, 75, 3, 34],
     [88, 2, 77, 73, 7, 63, 67],
     [99, 65, 4, 28, 6, 16, 70, 92],
     [41, 41, 26, 56, 83, 40, 80, 70, 33],
     [41, 48, 72, 33, 47, 32, 37, 16, 94, 29],
     [53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14],
     [70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57],
     [91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48],
     [63, 66, 4, 68,89, 53, 67, 30, 73, 16, 69, 87, 40, 31],
     [4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23]]

Why does s[1:][:-1] give me the same thing as s[1:] instead of (what I want) [s[i][:-1] for i in range(1,len(s))]. In other words, why does Python ignore my second index?

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

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

发布评论

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

评论(2

七婞 2024-09-08 06:58:49

Python 没有二维列表,它有列表的列表。我认为第一个 [1:] 提供了除第一个包含列表之外的所有内容,第二个 [:-1] 获取该结果并删除最后一个包含列表。

你想要的是:

[r[:-1] for r in s[1:]]

Python doesn't have 2-dimensional lists, it has lists of lists. I think the first [1:] gives everything but the first contained list, and the second [:-1] takes that result and removes the last contained list.

What you want is:

[r[:-1] for r in s[1:]]
飘过的浮云 2024-09-08 06:58:49

您错误地描述了结果:s[1:][:-1] 绝对与s[:1]相同,正如您错误地所说 - 相反,它与 s[1:-1] 相同。一探究竟!

这必须适用于任何列表s,无论其内容是什么(其他列表、字典、字符串、浮点数、独角兽......):s[1:] 表示“除了第一个之外的所有”,那么 [:-1] 表示“除了最后一个之外的所有”,所以显然它们的组合效果与 [1:-1] 表示“除了第一个和最后一个之外的所有”。括号中带有冒号的类似索引的语法也称为切片,当应用于一个列表(无论什么)时,它会返回另一个(通常更短)列表(也可以是任何列表)。

s 视为“锯齿状数组”,而不是它实际上的(只是一个列表,其项目碰巧也是列表 - 但某些或全部的类型项目的数量显然不能也不应该影响列表本身的操作语义,例如切片)可能会让您失望;也许是因为,如果第一个索引实际上是索引而不是切片,那么它的结果是原始列表的一个 item - 一个“whatever”(在您的情况下是一个整数列表),而不是一个任何东西的清单。因此,如果您随后应用进一步的索引或切片,您将在原始子列表之一上执行此操作 - 完全不同的事情。

@Mark 的答案已经展示了规范的列表理解解决方案来完成您真正想要的操作。我认为其他方法,如果您有 matlab 代码并想要等效的 Python 代码,可能包括 OMPC (但我我自己没试过)。

You're misdescribing the results: s[1:][:-1] is definitely not the same as s[:1], as you erroneously say -- it is, rather, the same as s[1:-1]. Check it out!

This must necessarily hold true of any list s, no matter what its contents may be (other lists, dicts, strings, floats, unicorns, ...): s[1:] means "all but the first", then [:-1] means "all but the last", so obviously their combined effects are the same as [1:-1] which means "all but the first and last". The indexing-like syntax with a colon in the brackets is also known as slicing, and when applied to a list (of whatevers) it returns another (typically shorter) list (also of whatevers).

Thinking of s as a "jagged array" rather than what it actually is (just a list, whose items happen to also be lists -- but the type of some or all of the items obviously can't and shouldn't affect the semantics of operations on the list itself, like slicing) may be what's throwing you off; perhaps because, if the first indexing is actually an indexing and not a slicing, its results is an item of the original list -- a "whatever" (a list of ints in your case), not a list of whatevers. So if you then apply a further indexing or slicing you're doing that on one of the original sublists -- a very different matter.

@Mark's answer has already shown the canonical list-comprehension solution to do what you actually want. I think that other approaches, if you have matlab code and want the Python equivalent, might include OMPC (but I haven't tried that myself).

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