Python 中索引和切片的区别
在本文中,我们将向您解释 Python 中索引和切片的区别。
索引和切片仅适用于序列数据类型。序列类型中元素插入的顺序是保留的,允许我们通过索引和切片访问其元素。
总结一下,Python 的序列类型有列表、元组、字符串、范围、字节和字节数组。索引和切片适用于所有这些类型。
索引
术语"索引"是指根据可迭代对象中元素的位置来引用可迭代对象的元素。
索引从 0 开始。序列中的第一个元素由索引 0 表示。
负索引从 -1 开始。序列中的最后一个元素由索引 -1 表示。
字符串中的每个字符都对应一个索引号,每个字符都可以通过其索引号访问。有两种方法可以访问字符串中的字符
使用正索引访问字符串字符
使用负索引访问字符串字符
T U T O T I A L S
Positive Indexing 0 1 2 3 4 5 6 7 8
Negative indexing -9 -8 -7 -6 -5 -4 -3 -2 -1
使用正索引访问字符串字符
在本例中,我们在方括号中传递一个正索引(我们希望访问)。索引号序列从索引号 0 开始。(表示字符串的第一个字符)。
示例
# input string inputString = "Hello tutorialspoint python" print("0th index character:", inputString[0]) print("7th index character", inputString[7]) print("12th index character:", inputString[12])('0th index character:', 'H') ('7th index character', 'u') ('12th index character:', 'a')
输出
0th index character: H 7th index character u 12th index character: a
使用负索引访问字符串字符
在这种类型的索引中,我们将负索引(我们希望访问)传递到方括号中。在这种情况下,索引号从 -1 开始(代表字符串的最后一个字符)。
示例
# input string inputString = "Hello tutorialspoint python" print("last index character:", inputString[-1]) print("6th index character from last:", inputString[-6])('last index character:', 'n') ('6th index character from last:', 'p')
输出
last index character: n 6th index character from last: p
Indexing in List
示例
# input list inputList =[1, 4, 8, 6, 2] print("Element at index 2:", inputList[2]) print("last element of an input list:", inputList[-1])('Element at index 2:', 8) ('last element of an input list:', 2)
输出
Element at index 2: 8 last element of an input list: 2
NOTE
当我们尝试使用不存在或太大的索引时,它会抛出IndexError
示例
# input list inputList =[1, 4, 8, 6, 2] # 打印输入列表中索引 10 处的元素 # 抛出 IndexError,因为索引 10 在输入列表中不存在 print("Element at index 10:", inputList[10])Traceback (most recent call last): File "main.py", line 5, in <module> print("Element at index 10:", inputList[10]) IndexError: list index out of range
输出
Traceback (most recent call last):
File "main.py", line 5, in <module>
print("Element at index 10:", inputList[10])
IndexError: list index out of range
切片
术语"切片"是指根据元素的索引从可迭代对象中获取元素的子集。
我们通过切片字符串来创建子字符串,该字符串实际上是存在于另一个字符串中的字符串。当我们只需要字符串的一部分而不是整个字符串时,我们会使用切片。
语法
string[start : end : step]
参数
start - 从哪里开始的索引 end - 结束索引 step - 之间要采取的跳转/增量数,即步长
字符串切片
# input string inputString = "Hello tutorialspoint python" print("First 4 characters of the string:", inputString[: 4]) print("Alternate characters from 1 to 10 index(excluded):", inputString[1 : 10 : 2]) print("Alternate characters in reverse order from 1 to 10 index(excluded):", inputString[-1 : -10 : -2])('First 4 characters of the string:', 'Hell') ('Alternate characters from 1 to 10 index(excluded):', 'el uo') ('Alternate characters in reverse order from 1 to 10 index(excluded):', 'nhy n')
输出
字符串的前 4 个字符:Hell 从 1 到 10 索引的替代字符(不包括):el uo 从 1 到 10 索引的反向替代字符(不包括):nhy n
元组切片
我们可以使用元组切片。它类似于我们使用字符串和列表的方式。元组切片用于获取各种项目。我们还使用切片运算符来执行元组切片。切片运算符可以用以下语法表示
语法
[start:stop:step]
示例
# Input tuple givenTuple = ("Welcome", "this", "is", "TutorialsPoint", "Website", 10) # Slicing with start and stop values(indices) print('Tuple slicing from index 1 to index 6 :', givenTuple[1:6]) # Slicing with only stop values(indices) print("Tuple slicing till index 7: ", givenTuple[:7]) # Slicing with only start value(indices) print("Tuple slicing from index 2 is:", givenTuple[2:]) # Slicing without any start and stop values print("Tuple slicing without any start and stop values:", givenTuple[:]) # Slicing in reverse order print("Tuple slicing in reverse order:", givenTuple[::-1])('Tuple slicing from index 1 to index 6 :', ('this', 'is', 'TutorialsPoint', 'Website', 10)) ('Tuple slicing till index 7: ', ('Welcome', 'this', 'is', 'TutorialsPoint', 'Website', 10)) ('Tuple slicing from index 2 is:', ('is', 'TutorialsPoint', 'Website', 10)) ('Tuple slicing without any start and stop values:', ('Welcome', 'this', 'is', 'TutorialsPoint', 'Website', 10)) ('Tuple slicing in reverse order:', (10, 'Website', 'TutorialsPoint', 'is', 'this', 'Welcome'))
输出
Tuple slicing from index 1 to index 6 : ('this', 'is', 'TutorialsPoint', 'Website', 10)
Tuple slicing till index 7: ('Welcome', 'this', 'is', 'TutorialsPoint', 'Website', 10)
Tuple slicing from index 2 is: ('is', 'TutorialsPoint', 'Website', 10)
Tuple slicing without any start and stop values: ('Welcome', 'this', 'is', 'TutorialsPoint', 'Website', 10)
Tuple slicing in reverse order: (10, 'Website', 'TutorialsPoint', 'is', 'this', 'Welcome')
索引与切片的区别
下表显示了 Python 中索引和切片之间的主要区别 −
| 索引 | 切片 |
|---|---|
| 它只返回 1 个项目 | 它返回一个新的列表/元组 |
| 如果您尝试使用过大的索引,将引发 IndexError。 | 用于切片时,超出范围的索引会得到温和处理。 |
| 我们无法通过索引中的项目分配来更改列表的长度。 | 我们可以通过将项目分配给切片来更改列表的长度,甚至清除它。 |
| 我们可以将单个元素或可迭代对象分配给索引。 | 当我们将单个元素分配给切片时,我们会得到一个 TypeError。它只接受可迭代对象。 |
结论
通过示例,我们了解了 Python 中索引和切片之间的区别。
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

