Python 语法/列表切片问题:这个语法是什么意思?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
切片采用
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 文档的数据模型页面:
Python 2.3 文档的“新增内容”部分也讨论了它们,当它们被添加到语言中时。
A slice takes the form
o[start:stop:step]
, all of which are optional.start
defaults to0
, the first index.stop
defaults tolen(o)
, the closed upper bound on the indicies of the list.step
defaults to1
, 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. Astop
of-2
indicates to exclude the last two elements. Soo[:-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:
The "what's new" section of the Python 2.3 documentation discusses them as well, when they were added to the language.
理解切片语法的一个好方法是将其视为等效
for
循环的语法糖。例如:相当于(例如,在 C 语言中):
其中
a
默认为0
,b
默认为len(L)
,c
默认为1
。(如果步长
c
是负数,则a
和b
的默认值会颠倒。这会给出合理的结果对于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:Is equivalent to (e.g., in C):
Where
a
defaults to0
,b
defaults tolen(L)
, andc
defaults to1
.(And if
c
, the step, is a negative number, then the default values ofa
andb
are reversed. This gives a sensible result forL[::-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.如果
list
是一个列表,则list[-1]
是列表的最后一个元素,list[-2]
是它前面的元素等等。另外,
list[a:b]
表示列表,其中list
中的所有元素位于a
和b
之间的位置。如果缺少其中之一,则假定表示列表末尾。因此,list[2:]
是从list[2]
开始的所有元素的列表。list[:-2]
是从list[0]
到list[-2]
的所有元素的列表。在您的代码中,
[0:-1]
的部分与[:-1]
相同。If
list
is a list thenlist[-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 inlist
at positions betweena
andb
. 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 fromlist[2]
. Andlist[:-2]
is the list of all elements fromlist[0]
tolist[-2]
.In your code, the
[0:-1]
part it the same as[:-1]
.