Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PYTHON-What';s是[1:-1]的输出_Python - Fatal编程技术网

PYTHON-What';s是[1:-1]的输出

PYTHON-What';s是[1:-1]的输出,python,Python,我想知道为什么下面代码的输出(2,4)是正确的?我知道2是从哪里来的,但不是4。这是因为[:-1]只是[:len(t)-2]的省略吗 print(t[1:-1])将从第二个对象[1:][/code>打印到最后一个对象之前的对象[:-1] t = (1, 2, 4, 3) print(t[1 : -1]) (t[1:-1])是1,是带有列表第二个元素的开始索引。 [1:-1]是列表中的最后一个元素 t = (1, 2, 4, 3) print(t[1 : -1])

我想知道为什么下面代码的输出(2,4)是正确的?我知道2是从哪里来的,但不是4。这是因为[:-1]只是[:len(t)-2]的省略吗

print(t[1:-1])
将从第二个对象
[1:][/code>打印到最后一个对象之前的对象
[:-1]

t = (1, 2, 4, 3) 
print(t[1 : -1])
(t[1:-1])
是1,是带有列表第二个元素的开始索引。
[1:-1]
是列表中的最后一个元素

t = (1, 2, 4, 3) 
print(t[1 : -1])