[::-1] 在 Python 与例子
[::-1] 中的内容 Python?
Python 在使用 1 时有许多不同的语法和应用。它可以表示为 [-1]
,也可以描述为 [::-1]
.
上述符号中的方括号通常指定列表中的多个项目。使用方括号时,可以指定列表中的多个项目或单个项目。
每当程序员定义 [::-1] 时,它都表明程序必须在给定的列表中从头到尾遍历。您可以在 Python 中进行索引,这有助于对可迭代序列(例如列表或字符串)进行切片和切块。
中的 1 的类型是什么 Python?
在 Python 中,有两种表示 1 的方法,如下所述:–
- 它可以用没有冒号的简单方括号来表示,也可以用双冒号语法来表示。
- 可以使用 [] 括号将可迭代序列(例如列表或字符串)解析为项目级别。
- 在 [] 括号内,必须提供不同的索引。
以下是列表的语法,如下所示:-
句法: -
SEQUENCE [START: STOP: STEP]
上述语法是一般表示。没有冒号的方括号的语法表示如下:
句法: -
SEQUENCE [-1]
带有两个冒号的方括号的语法如下所示:
句法: -
SEQUENCE[::-1]
解释: -
- start 参数表示起始索引号。
- stop 参数代表结束或最后一个索引号。
- 参数 step 是可选参数,用于提供选择的增量。在语法中,步数初始化为 -1。
- 在上面的语法中,序列可以是字符串、列表或字典。
a[-1] 和 a[::-1] 之间的区别 Python
[-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 下的索引始终从零开始。
切片可以通过两种基本形式实现。
- 第一种形式是提供序列的起始和终止索引参数。这样,序列将返回序列起始和终止 [end -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 从最后一个元素后退并向上移动到第一个元素,从而得到一个反向列表或字符串。
示例 1 Python
切片或索引可用于从较大的列表中提取较小的列表。同样,它还可用于从较大的字符串中提取子字符串。
让我们举一个例子来说明如何在 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 支持零索引系统。