Python 中的 [::-1] 示例
Python 中的 [::-1] 是什么意思?
Python 在使用 1 时有许多不同的语法和应用。它可以表示为 [-1]
,也可以描述为 [::-1]
。
上面表示法中的方括号通常用于指定列表中的多个项。在使用方括号时,可以指定多个项或一个项。
每当程序员定义 [::-1] 时,它都表示程序必须在给定列表中从头到尾进行遍历。您可以在 Python 中进行索引,这有助于对列表或字符串等可迭代序列进行切片。
Python 中的 1 的类型是什么?
如下有两种表示 Python 中 1 的方法:–
- 它可以表示为没有冒号的简单方括号,另一种方法是使用双冒号语法。
- 列表或字符串等可迭代序列可以使用 [] 方括号逐项解析。
- 在 [] 方括号内,必须提供不同的索引。
以下是列表的语法,如下所示:–
语法:–
SEQUENCE [START: STOP: STEP]
上述语法是通用表示。没有冒号的方括号语法表示如下:
语法:–
SEQUENCE [-1]
带有两个冒号的方括号语法表示如下:
语法:–
SEQUENCE[::-1]
解释:–
- start 参数表示起始索引号。
- stop 参数表示结束或最后一个索引号。
- step 参数是一个可选参数,用于提供选择的增量。在语法中,step 计数初始化为 -1。
- 序列可以是上述语法中的字符串、列表或字典。
Python 中 a[-1] 和 a[::-1] 之间的区别
A [-1] 用于负索引,并帮助在给定列表中反向选择项。它表示从反向顺序的末尾开始列表。这里,[-1] 表示来自最后一个位置的第一项。以下是一个解释 A[-1] 用法的示例
示例:–
Python 代码
b= [7,4,5,6] print ("The list is", b) print (" The last element in the list is", b [-1])
输出
The list is [7, 4, 5, 6] The last element in the list is 6
A[::-1] 和 A[-1] 在执行方面看起来相似,但在语法方面有所不同。第一个语法有三个参数,而后者语法只有一个参数。
A[::-1] 中的前两个参数表示列表的开始和停止。如果省略开始和停止参数,它将返回列表中的所有项。
最后一个参数显示反向输出。此参数是可选的。
以下是解释 A[::-1] 用法的示例
示例:–
Python 代码
b= [7,4,5,6] print ("The list is", b) print (" The reversed list as determined is", b [::-1])
输出
The list is [7, 4, 5, 6] The reversed list as determined is [6, 5, 4, 7]
解释
上面的 Python 代码不会为创建的列表产生相同的结果。相反,它为提供的基础列表提供了反向列表。
如何在 Python 中执行索引?
为了访问可迭代 Python 列表中的项,需要对列表进行索引或使用应用于列表中项位置的索引。
Python 对列表执行零基索引。对于零基索引,列表中的第一项被分配为位置 0。
让我们看一个示例,尝试访问 Python 列表中的开头和第二项。
示例:–
Python 代码
b= [7,4,5,6] print ("The list is", b) print (" The reverse ordered list as determined is", b [::-1]) print (" The second item found in list is", b [1]) print (" The first or beginning item in the list is", b [0])
输出
The list is [7, 4, 5, 6] The reverse ordered list as determined is [6, 5, 4, 7] The second found item in the list is 4 The first or beginning item in the list is 7
解释
如上代码所示,要访问列表中的第一个元素,0 被用作列表类型方括号内的索引。类似地,1 被用作列表类型方括号内的索引以访问列表的第二个元素。列表的索引也可以应用于字典、字符串和元组。
让我们以 Python 中 字符串类型的索引为例。
示例
Python 代码
b= "GURU99" print ("The list is", b) print (" The reverse ordered list as determined is", b [::-1]) print (" The second item found in list is", b [1]) print (" The first or beginning item in the list is", b [0])
输出
The list is GURU99 The reverse ordered list as determined is 99URUG The second item found in the list is U The first or beginning item in the list is G
解释
如上所示,输出共享了字符串“GURU99”的第一个和第二个元素。字符串必须使用双引号初始化。Python 还允许我们对可迭代列表、字符串或字典执行负索引。
通过提供负索引,可以访问列表的最后一个和倒数第二个元素。让我们看一个字符串负索引的示例,如下所示。
示例:–
Python 代码
b= "GURU99" print ("The list is", b) print (" The reverse ordered list as determined is", b [::-1]) print (" The second item found in list is", b [-2]) print (" The first or beginning item in the list is", b [-1])
输出
The list is GURU99 The reverse ordered list as determined is 99URUG The second item found in the list is 9 The first or beginning item in the list is 9
切片在 Python 中的作用
简单来说,切片就是剪切东西。它允许在 Python 中切片列表。
切片在 Python 中的重要性
- 它允许访问可迭代列表或字符串的特定元素。
- 它还有助于删除和修改字符串或列表。
- 它使可迭代序列简洁易读。
- 与索引概念下的单个元素相比,它允许我们访问多个项。
- 它使用索引在可迭代序列中获取特定的项范围。
- Python 中的索引始终是零基的,无论是在可迭代序列上进行切片还是索引。
切片有两种基本形式。
- 第一种形式是提供序列的开始和停止索引参数。通过这样做,序列将返回序列开始到停止 [结束 -1] 之间的所有可能项。
- 第二种方法是提供序列的开始和停止索引参数以及步长。步长允许在开始和停止之间的项范围内返回特定或选定的项。
以下是第一种切片形式的语法:–
语法
SEQUENCE [START: STOP]
以下是第二种切片形式的语法:–
语法
SEQUENCE [START: STOP: STEP]
让我们以如下所示的第一种切片形式为例。
示例
Python 代码
b= "GURU99" print ("The list is", b) Substring=b[0:3] print (" The substring in the list is", Substring)
输出
The list is GURU99 The substring in the list is GUR
让我们以如下所示的第二种切片形式为例。
示例
Python 代码
b= "GURU99" print ("The list is", b) Substring=b[0:9:2] print (" The substring in the list is", Substring)
输出
The list is GURU99 The substring in the list is GR9
Python 还对可迭代序列执行负切片。它允许您从序列的末尾访问一系列项或元素。
让我们举例说明如何从序列中访问最后三个元素,如下所示。
示例
Python 代码
b= "GURU99" print ("The list is", b) Substring=b[-5:-1] print (" The substring in the list is", Substring)
输出
The list is GURU99 The substring in the list is URU9
如何使用 Python 中的 1 反转 Python 列表?
负切片和负索引可用于在 Python 中反转字符串或列表。让我们以名为“GURU99”的字符串为例进行说明。
示例
Python 代码
b= "GURU99" print ("The list is", b) Substring=b[::-1] print (" The reverse of string GURU99 is", Substring)
输出
The list is GURU99 The substring in the list is 99URUG
解释
在这里,代码从最后一个位置获取所有字符串元素。它从 -1 开始,并获取所有项或元素。上面的代码行告诉 Python 从最后一个元素向后步进,直到第一个元素,这会导致列表或字符串反转。
Python 中 1 的示例
切片或索引可用于从较大的列表中提取较小的列表。类似地,它可用于从较大的字符串中提取子字符串。
让我们举例说明如何在 Python 中使用 1 从较大的列表中提取较小的列表
示例
Python 代码
b = [1, 2, 3, 4, 5, 6, 7] slist = b[1::] print("the smaller list from larger list is:",slist)
输出
the smaller list from the larger list is: [2, 3, 4, 5, 6, 7]
解释
在上面的代码中,跳过第一个索引,Python 会提取从列表的第 2 个位置开始的所有元素,并创建一个较小的列表。发生这种情况是因为 Python 在将索引应用于列出的元素位置时遵循零索引。
摘要
- Python 使用 1 来执行列表、字符串和字典的索引和切片。
- Python 中有三种序列类型。
- 可迭代序列可以是列表、字符串或字典。这些是内置对象类型。
- Python 支持负索引和正索引。
- 它还支持负切片和正切片。
- a[-1] 和 a[::-1] 的表示在语法和逻辑上都存在差异
- A[-1] 提供列表的最后一个元素。
- A[::-1] 从列表的最后一个元素开始提供所有元素。此命令有助于反转可迭代序列。
- Python 支持零索引系统。