简体   繁体   English

在line.split('+')[ - 1]中,方括号中的-1表示在Python中

[英]In line.split('+')[-1] what does the -1 in the square brackets indicate in Python

Let's assume that we have this code: 我们假设我们有这个代码:

name = line.split('+')[-1]

What does the -1 do? -1做什么? 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] ? 如果有[0][1]会有什么区别?

The line of code you gave is basically doing three things: 你给出的代码行基本上做了三件事:

  1. It takes the string line and splits it on + 's using str.split . 它使用字符串line并使用str.split将其拆分为+ This will return a list of substrings: 这将返回一个子串列表:

     >>> line = 'a+b+c+d' >>> line.split('+') ['a', 'b', 'c', 'd'] >>> 
  2. The [-1] then indexes that list at position -1 . 然后[-1]将位于-1列表编入索引。 Doing so will return the last item: 这样做将返回最后一项:

     >>> ['a', 'b', 'c', 'd'][-1] 'd' >>> 
  3. It takes this item and assigns it as a value for the variable name . 它接受此项并将其指定为变量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'
>>>

str.split returns 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教程 (包含索引,切片)。

Negative indexes in Python are syntactic sugar for accessing the elements in reverse order, from right-to-left, starting in -1 . Python中的负索引是以相反的顺序访问元素的语法糖,从右到左,从-1开始。 So -1 is the last item, -2 is the second-to-last item, and so on - the first item would be lst[-len(lst)] . 所以-1是最后一项, -2是倒数第二项,依此类推 - 第一项是lst[-len(lst)] For example: 例如:

lst = [1, 2, 3]
lst[-1]
=> 3
lst[-2]
=> 2
lst[-3]
=> 1

Split将创建列表,然后使用[-1]获取最后一个元素

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('+') 给定下面的字符串,让我们将其拆分:•line ='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.eg 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开头(这是list.eg名称[0]的第一个元素正在访问列表是'a')并且它从左到右操作,而负索引从-1开始,从右到左(在上面的例子中'd'是索引-1)使用你的示例问题,name = line.split ('+')[ - 1],这将返回列表中的最后一项,即'd'name = line.split('+')[0],这将返回列表中的第一项,即' 'name = line.split('+')[1],这将返回列表中的第二项,即'b'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM