在python中,-1是什么意思?(复制)

时间:2021-09-14 14:13:54

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 'message starts with <stream:stream then something needs to be appended. However I can't seem to figure out where it should be appended. I have absolutely no idea what :-1 indicates. I did several Google searches with no result.

我理解如果“消息以 开头,那么需要添加一些东西。然而,我似乎想不出应该把它附加在什么地方。我完全不知道-1代表什么。我做了几个谷歌搜索,没有结果。

Would somebody be so kind as to explain what this does?

有人能解释一下这是怎么回事吗?

5 个解决方案

#1


21  

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[1:2]
[2]

a[start:]

(开始:)

>>> a[1:]
[2, 3, 4, 5, 6]

a[:end]
Your case

(结束):你的案子

>>> a = [1,2,3,4,5,6]
>>> a[:-1]
[1, 2, 3, 4, 5]

a[:]

[:]

>>> a[:]
[1, 2, 3, 4, 5, 6]

#2


3  

It's called slicing, and it returns everything of message but the last element.

它被称为切片,它返回除了最后一个元素之外的所有消息。

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 -1 with any number:

你可以用任何数字替换-1:

In [4]: "Hello World"[:2] # Indexes starting from 0
Out[4]: "He"

The last index is not included.

最后一个索引不包括在内。

#3


1  

It's called 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 #片

You'll notice it's similar to the range arguments, and the : part returns the entire iterable, so the -1 is everything except the last index.

您会注意到它与range参数相似,并且:part返回整个可迭代的值,所以-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] 

#4


0  

It returns message without the last element. If message is a string, message[:-1] drops the last character.

它返回没有最后一个元素的消息。如果消息是字符串,则消息[:-1]将删除最后一个字符。

See the tutorial.

看教程。

#5


0  

To answer your case directly:

直接回答你的问题:

if message.startswith('<stream:stream'): message = message[:-1] + ' />'

This basically checks, if message starts with <stream:stream, and if that is the case it will drop the last character and add a ' />' instead.

这基本上是检查,如果消息以 '。

So, as your message is an XML string, it will make the element an empty element, closing itself.

因此,由于您的消息是一个XML字符串,它将使元素成为一个空元素,并关闭自己。

#1


21  

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[1:2]
[2]

a[start:]

(开始:)

>>> a[1:]
[2, 3, 4, 5, 6]

a[:end]
Your case

(结束):你的案子

>>> a = [1,2,3,4,5,6]
>>> a[:-1]
[1, 2, 3, 4, 5]

a[:]

[:]

>>> a[:]
[1, 2, 3, 4, 5, 6]

#2


3  

It's called slicing, and it returns everything of message but the last element.

它被称为切片,它返回除了最后一个元素之外的所有消息。

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 -1 with any number:

你可以用任何数字替换-1:

In [4]: "Hello World"[:2] # Indexes starting from 0
Out[4]: "He"

The last index is not included.

最后一个索引不包括在内。

#3


1  

It's called 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 #片

You'll notice it's similar to the range arguments, and the : part returns the entire iterable, so the -1 is everything except the last index.

您会注意到它与range参数相似,并且:part返回整个可迭代的值,所以-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] 

#4


0  

It returns message without the last element. If message is a string, message[:-1] drops the last character.

它返回没有最后一个元素的消息。如果消息是字符串,则消息[:-1]将删除最后一个字符。

See the tutorial.

看教程。

#5


0  

To answer your case directly:

直接回答你的问题:

if message.startswith('<stream:stream'): message = message[:-1] + ' />'

This basically checks, if message starts with <stream:stream, and if that is the case it will drop the last character and add a ' />' instead.

这基本上是检查,如果消息以 '。

So, as your message is an XML string, it will make the element an empty element, closing itself.

因此,由于您的消息是一个XML字符串,它将使元素成为一个空元素,并关闭自己。