Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.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 列表和元组中相同操作的不同ValueError_Python_Python 3.x_List_Tuples - Fatal编程技术网

Python 列表和元组中相同操作的不同ValueError

Python 列表和元组中相同操作的不同ValueError,python,python-3.x,list,tuples,Python,Python 3.x,List,Tuples,我很好奇,为什么当我尝试获取索引时,列表和元组中的ValueErrors是不同的。列表的ValueError以良好的格式返回,实际参数ValueError:“ITEM”不在列表中,而tuple返回类似ValueError的内容:tuple.indexx:x不在tuple中。 我认为List和Tuple都在调用相同的索引方法,那么为什么它会产生不同的值错误呢 >>> jframe_li ['Angular', 'React', 'Vue.js', 'Ember.js', 'Me

我很好奇,为什么当我尝试获取索引时,列表和元组中的ValueErrors是不同的。列表的ValueError以良好的格式返回,实际参数ValueError:“ITEM”不在列表中,而tuple返回类似ValueError的内容:tuple.indexx:x不在tuple中。 我认为List和Tuple都在调用相同的索引方法,那么为什么它会产生不同的值错误呢


>>> jframe_li
['Angular', 'React', 'Vue.js', 'Ember.js', 'Mereor', 'Node.js', 'Backbone.js']
>>> jframe_tu
('Angular', 'React', 'Vue.js', 'Ember.js', 'Mereor', 'Node.js', 'Backbone.js')
>>> jframe_li.index('React')
1
>>> jframe_tu.index('React')
1
>>> jframe_li.index('react')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 'react' is not in list

>>> jframe_tu.index('react')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: tuple.index(x): x not in tuple

列表和元组的索引方法在实现上存在差异,包括在引发的ValueError的文本中


参见和

@snakecharmerb根据文档,元组实现了所有常见的序列操作,包括.index,现在你会说它以不同的方式实现它吗?是的,你是对的,我在过程中也学到了。