Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.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元组中的特定条目_Python_Tuples - Fatal编程技术网

访问python元组中的特定条目

访问python元组中的特定条目,python,tuples,Python,Tuples,在python元组中,我希望访问第一个条目。我可以通过写作来做到这一点: example_tuple[0] example_array[:-1] 现在,第一个条目是一个浮点数组。我想从这个数组中获取除最后一个之外的所有条目。像descibed一样,我写下: example_tuple[0] example_array[:-1] 现在我想堆叠这两个功能,并尝试使用以下命令: example_tuple[0[:-1]] 这会产生以下错误: TypeError: 'int' object i

在python元组中,我希望访问第一个条目。我可以通过写作来做到这一点:

example_tuple[0]
example_array[:-1]
现在,第一个条目是一个浮点数组。我想从这个数组中获取除最后一个之外的所有条目。像descibed一样,我写下:

example_tuple[0]
example_array[:-1]
现在我想堆叠这两个功能,并尝试使用以下命令:

example_tuple[0[:-1]]
这会产生以下错误:

TypeError: 'int' object is not subscriptable
我不确定出了什么问题,因为其中没有整数。

通过键入:

示例\u元组[0[:-1]]
Python首先尝试计算括号内的内容,即
0[:-1]
。这就解释了错误:

TypeError:“int”对象不可下标
您正试图以数组形式访问
0

正如@ShubhamShaswat所说,要访问数组中的一个数组,您需要获取第一个数组,并访问所需的值:

example_tuple=[[5.7,2.9,7.9],[0.1,4.2],[1.2]]
###使用2个步骤###
#温度var等于[5.7,2.9,7.9]
temp\u var=示例\u元组[0]
#产出:[5.7,2.9]
打印(临时变量[:-1])
###在Python中,可以通过组合数组访问来缩短###
#产出:[5.7,2.9]
打印(示例\u元组[0][:-1])

试试
示例组[0][:-1]
@ShubhamShaswat Yes。这对我来说非常合适。非常感谢。