Python 语法/列表切片问题:这个语法是什么意思?

发布于 2024-11-26 19:27:14 字数 240 浏览 2 评论 0原文

lines = file('info.csv','r').readlines()

counts = []
for i in xrange(4):
    counts.append(fromstring(lines[i][:-2],sep=',')[0:-1])

如果有人可以向我解释这段代码,我将不胜感激。我似乎找不到关于切片的更高级的示例——只有非常简单的示例,无法解释这种情况。

非常感谢。

lines = file('info.csv','r').readlines()

counts = []
for i in xrange(4):
    counts.append(fromstring(lines[i][:-2],sep=',')[0:-1])

If anyone can explain this code to me, it would be greatly appreciated. I can't seem to find more advanced examples on slicing--only very simple ones that don't explain this situation.

Thank you very much.

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

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

发布评论

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

评论(3

苄①跕圉湢 2024-12-03 19:27:14

切片采用 o[start:stop:step] 形式,所有这些都是可选的。 start 默认为 0,即第一个索引。 stop 默认为 len(o),即列表索引的闭合上限。 step 默认为 1,包括列表中的每个值。

如果指定负值,则表示距列表末尾的偏移量。例如,[-1] 访问列表中的最后一个元素,-2 访问倒数第二个元素。

如果您为步骤输入非 1 值,您将包含不同的元素或以不同的顺序包含它们。 2 将跳过所有其他元素。 3 会跳过三分之二。 -1 将向后浏览列表。

[:-2]

由于省略了 start,因此它默认为列表的开头。 stop-2 表示排除最后两个元素。因此,o[:-2] 对列表进行切片以排除最后两个元素。

[0:-1]

这里的 0 是多余的,因为无论如何它都是 start 的默认值。这与其他切片相同,只是它只排除最后一个元素。

来自 Python 2.7 文档的数据模型页面

序列还支持切片:a[i:j] 选择索引为 k 的所有项目,使得 i <= k << j。当用作表达式时,切片是相同类型的序列。这意味着索引集被重新编号,以便从 0 开始。

某些序列还支持带有第三个“step”参数的“扩展切片”:a[i:j:k] 选择索引为 x 的 a 的所有项目,其中x = i + n*k,n >= 0 且 i <= x < j。

Python 2.3 文档的“新增内容”部分也讨论了它们,当它们被添加到语言中时。

A slice takes the form o[start:stop:step], all of which are optional. start defaults to 0, the first index. stop defaults to len(o), the closed upper bound on the indicies of the list. step defaults to 1, including every value of the list.

If you specify a negative value, it represents an offset from the end of the list. For example, [-1] access the last element in a list, and -2 the second last.

If you enter a non-1 value for step, you will include different elements or include them in a different order. 2 would skip every other element. 3 would skip two out of every three. -1 would go backwards through the list.

[:-2]

Since start is omitted, it defaults to the beginning of the list. A stop of -2 indicates to exclude the last two elements. So o[:-2] slices the list to exclude the last two elements.

[0:-1]

The 0 here is redundant, because it's what start would have defaulted to anyway. This is the same as the other slice, except that it only excludes the last element.

From the Data model page of the Python 2.7 docs:

Sequences also support slicing: a[i:j] selects all items with index k such that i <= k < j. When used as an expression, a slice is a sequence of the same type. This implies that the index set is renumbered so that it starts at 0.

Some sequences also support “extended slicing” with a third “step” parameter: a[i:j:k] selects all items of a with index x where x = i + n*k, n >= 0 and i <= x < j.

The "what's new" section of the Python 2.3 documentation discusses them as well, when they were added to the language.

等风来 2024-12-03 19:27:14

理解切片语法的一个好方法是将其视为等效 for 循环的语法糖。例如:

L[a:b:c]

相当于(例如,在 C 语言中):

for(int i = a; i < b; i += c) {
    // slice contains L[i]
}

其中 a 默认为 0b 默认为 len(L)c 默认为 1

(如果步长 c 是负数,则 ab 的默认值会颠倒。这会给出合理的结果对于L[::-1])。

那么您唯一需要知道的另一件事是,在 Python 中,索引“环绕”,因此 L[-1] 表示列表中的最后项, L[-2] 是倒数第二个,依此类推。

A good way to understand the slice syntax is to think of it as syntactic sugar for the equivalent for loop. For example:

L[a:b:c]

Is equivalent to (e.g., in C):

for(int i = a; i < b; i += c) {
    // slice contains L[i]
}

Where a defaults to 0, b defaults to len(L), and c defaults to 1.

(And if c, the step, is a negative number, then the default values of a and b are reversed. This gives a sensible result for L[::-1]).

Then the only other thing you need to know is that, in Python, indexes "wrap around", so that L[-1] signifies the last item in the list, L[-2] is the second to last, and so forth.

幽蝶幻影 2024-12-03 19:27:14

如果 list 是一个列表,则 list[-1] 是列表的最后一个元素,list[-2] 是它前面的元素等等。

另外,list[a:b] 表示列表,其中 list 中的所有元素位于 ab 之间的位置。如果缺少其中之一,则假定表示列表末尾。因此,list[2:] 是从 list[2] 开始的所有元素的列表。 list[:-2] 是从 list[0]list[-2] 的所有元素的列表。

在您的代码中,[0:-1] 的部分与 [:-1] 相同。

If list is a list then list[-1] is the last element of the list, list[-2] is the element before it and so on.

Also, list[a:b] means the list with all elements in list at positions between a and b. If one of them is missing, it is assumed to mean the end of the list. Thus, list[2:] is the list of all elements starting from list[2]. And list[:-2] is the list of all elements from list[0] to list[-2].

In your code, the [0:-1] part it the same as [:-1].

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