:-1 在 python 中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14419206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does :-1 mean in python?
提问by Swen Kooij
Possible Duplicate:
The Python Slice Notation
可能的重复:
Python 切片符号
I'm trying to port some Python code to C, but I came across this line and I can't figure out what it means:
我正在尝试将一些 Python 代码移植到 C,但我遇到了这一行,我无法弄清楚它的含义:
if message.startswith('<stream:stream'):
message = message[:-1] + ' />'
I understand that if 'messagestarts with <stream:streamthen something needs to be appended. However I can't seem to figure out where it should be appended. I have absolutely no idea what :-1indicates. I did several Google searches with no result.
我知道如果 'message开头,<stream:stream则需要附加一些内容。但是我似乎无法弄清楚它应该附加在哪里。我完全不知道什么:-1表明。我做了几次谷歌搜索都没有结果。
Would somebody be so kind as to explain what this does?
有人会这么好心解释这是什么吗?
采纳答案by xlharambe
It is list indexing, it returns all elements [:]except the last one -1. Similar question here
它是列表索引,它返回[:]除最后一个之外的所有元素-1。类似的问题在这里
For example,
例如,
>>> a = [1,2,3,4,5,6]
>>> a[:-1]
[1, 2, 3, 4, 5]
It works like this
它是这样工作的
a[start:end]
a[start:end]
>>> a[1:2]
[2]
a[start:]
a[start:]
>>> a[1:]
[2, 3, 4, 5, 6]
a[:end]
Your case
a[:end]
你的情况
>>> a = [1,2,3,4,5,6]
>>> a[:-1]
[1, 2, 3, 4, 5]
a[:]
a[:]
>>> a[:]
[1, 2, 3, 4, 5, 6]
回答by Hymancogdill
It's called slicing
它被称为 slicing
"Return a slice object representing the set of indices specified by range(start, stop, step)."
-from this link: http://docs.python.org/2/library/functions.html#slice
“返回一个切片对象,表示由范围(开始,停止,步骤)指定的一组索引。”
- 从这个链接:http: //docs.python.org/2/library/functions.html#slice
You'll notice it's similar to the range arguments, and the :part returns the entire iterable, so the -1is everything except the last index.
您会注意到它类似于范围参数,并且该:部分返回整个可迭代对象,因此-1除了最后一个索引之外的所有内容。
Here is some basic functionality of slicing:
以下是切片的一些基本功能:
>>> s = 'Hello, World'
>>> s[:-1]
'Hello, Worl'
>>> s[:]
'Hello, World'
>>> s[1:]
'ello, World'
>>> s[5]
','
>>>
Follows these arguments:
遵循以下论点:
a[start:stop:step]
Or
或者
a[start:stop, i]
回答by NPE
It returns messagewithout the last element. If messageis a string, message[:-1]drops the last character.
它返回message没有最后一个元素。如果message是字符串,则message[:-1]删除最后一个字符。
See the tutorial.
请参阅教程。
回答by NPE
It's called slicing, and it returns everything of messagebut the last element.
这称为切片,它返回message除最后一个元素之外的所有内容。
Best way to understand this is with example:
理解这一点的最佳方法是举例:
In [1]: [1, 2, 3, 4][:-1]
Out[1]: [1, 2, 3]
In [2]: "Hello"[:-1]
Out[2]: "Hell"
You can always replace -1with any number:
您可以随时替换-1为任何数字:
In [4]: "Hello World"[:2] # Indexes starting from 0
Out[4]: "He"
The last index is not included.
不包括最后一个索引。
回答by poke
To answer your case directly:
要直接回答您的情况:
if message.startswith('<stream:stream'): message = message[:-1] + ' />'
This basically checks, if messagestarts with <stream:stream, and if that is the case it will drop the last character and add a ' />'instead.
这基本上会检查是否message以 开头<stream:stream,如果是这样,它将删除最后一个字符并添加一个' />'。
So, as your message is an XML string, it will make the element an empty element, closing itself.
因此,由于您的消息是一个 XML 字符串,它将使该元素成为一个空元素,从而关闭自身。

