在 line.split('+')[-1] 中,方括号中的 -1 在 Python 中表示什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21462879/
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
In line.split('+')[-1] what does the -1 in the square brackets indicate in Python
提问by user3245453
Let's assume that we have this code:
让我们假设我们有这个代码:
name = line.split('+')[-1]
What does the -1 do? I have seen it in various codes but not sure on what it does?
And what would the difference be if there was a [0]or a [1]?
-1 有什么作用?我在各种代码中看到过它,但不确定它的作用是什么?如果有 a[0]或 a会有什么区别[1]?
采纳答案by user3245453
The line of code you gave is basically doing three things:
您提供的这行代码基本上做了三件事:
It takes the string
lineand splits it on+'s usingstr.split. This will return a list of substrings:>>> line = 'a+b+c+d' >>> line.split('+') ['a', 'b', 'c', 'd'] >>>The
[-1]then indexes that list at position-1. Doing so will return the last item:>>> ['a', 'b', 'c', 'd'][-1] 'd' >>>It takes this item and assigns it as a value for the variable
name.
它获取字符串
line并将其拆分为+'s usingstr.split。这将返回子字符串列表:>>> line = 'a+b+c+d' >>> line.split('+') ['a', 'b', 'c', 'd'] >>>在
[-1]随后指数在位置列表-1。这样做将返回最后一项:>>> ['a', 'b', 'c', 'd'][-1] 'd' >>>它接受此项并将其分配为变量的值
name。
Below is a more complete demonstration of the concepts mentioned above:
下面是对上述概念的更完整的演示:
>>> line = 'a+b+c+d'
>>> line.split('+')
['a', 'b', 'c', 'd']
>>> lst = line.split('+')
>>> lst[-1]
'd'
>>> lst[0]
'a'
>>> lst[1]
'b'
>>> lst[2]
'c'
>>> lst[3]
'd'
>>>
回答by falsetru
str.splitreturns a list:
str.split返回一个列表:
>>> '1+2+3'.split('+')
['1', '2', '3']
list[-1]yields the last item (negative index starts from -1)
list[-1]产生最后一项(负索引从 -1 开始)
>>> '1+2+3'.split('+')[-1]
'3'
>>> '1+2+3'.split('+')[0] # the first item (Python index starts from 0)
'1'
>>> '1+2+3'.split('+')[1]
'2'
See Lists- Python tutorial(contains indexing, slicing).
请参阅Lists- Python 教程(包含索引、切片)。
回答by Jayaram
Splitwill create list and from that you are getting the last element using [-1]
Split将创建列表,并从中获取最后一个元素 [-1]
回答by óscar López
Negative indexes in Python are syntactic sugar for accessing the elements in reverse order, from right-to-left, starting in -1. So -1is the last item, -2is the second-to-last item, and so on - the first item would be lst[-len(lst)]. For example:
Python 中的负索引是一种语法糖,用于以相反的顺序访问元素,从右到左,从-1. 所以-1是最后一项,倒数-2第二项,依此类推 - 第一项是lst[-len(lst)]. 例如:
lst = [1, 2, 3]
lst[-1]
=> 3
lst[-2]
=> 2
lst[-3]
=> 1
回答by Carolyne Mwende
Okay, in order to understand what is happening here, we need to understand lists, split() and slicing functions of a list.
好的,为了理解这里发生的事情,我们需要了解列表、列表的 split() 和切片函数。
For example:
例如:
? Given the string below, let us split it: ? line ='a+b+c+d' ? name=line.split(‘+')
? 给定下面的字符串,让我们拆分它:行 ='a+b+c+d' ? name=line.split('+')
? After splitting the string, it becomes a list. As shown below ? ['a','b','c','d']
? 字符串拆分后,就变成了一个列表。如下所示 ?['A B C D']
? Please note: '+' is called the separator : meaning, the string will be separated based on the number of '+' available, hence the list above.
? 请注意:'+' 被称为分隔符:意思是,字符串将根据可用的 '+' 的数量进行分隔,因此是上面的列表。
? Square brackets after the separator(always in normal parenthesis) is accessing the elements in the list Lists are accessed using index, positive index begins with 0 (which is the first element of a list.e.g name[0] is accessing first element of the list which is ‘a') and it operates from left to right while negative indexes starts from -1, from right to left(in the example above 'd' is index -1) Using your sample question, name = line.split('+')[-1] , this will return last item in the list, that is ‘d' name = line.split('+')[0], this will return first item in the list, that is ‘a' name = line.split('+')[1], this will return second item in the list, that is ‘b'
? 分隔符后的方括号(总是在正常括号中)访问列表中的元素列表使用索引访问,正索引从 0 开始(这是列表的第一个元素。例如 name[0] 访问列表的第一个元素list 是 'a') 并且它从左到右操作,而负索引从 -1 开始,从右到左(在上面的示例中,'d' 是索引 -1)使用您的示例问题,name = line.split( '+')[-1] ,这将返回列表中的最后一项,即 'd' name = line.split('+')[0] ,这将返回列表中的第一项,即 'a ' name = line.split('+')[1], 这将返回列表中的第二项,即 'b'

