python - List 和 Tuple 中相同操作的不同 ValueError
问题描述
当我尝试获取索引时,我很好奇为什么 List 和 Tuple 中的 ValueErrors 不同。列表的 ValueError 以良好的格式返回,实际参数“ValueError:'ITEM'不在列表中”,而元组返回类似“ValueError:tuple.index(x):x 不在元组中”的内容。我认为 List 和 Tuple 都在调用相同的 index() 方法,那么为什么它会引发不同的 ValueErrors?
>>> 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
解决方案
index
列表和元组的方法存在实现差异,包括 raise 的文本ValueError
。
请参阅tuple.index 的ValueError字符串和 list.index 的 ValueError 字符串
推荐阅读
- android - Android 检查上下文中的 docker uild 错误:没有从 /config/sdcardfs/remove_userid 读取的权限
- reactjs - 从 MongoDB 数据库保存和获取媒体
- google-apps-script - 复选框标记为true时如何将行删除到新工作表
- c# - 我们如何以编程方式在 Azure DevOps 中添加现有链接
- java - 如何使用 jar 文件为 linux 制作安装程序?
- javascript - Firebase 身份验证未将数据存储在本地存储中
- ios - 将.a静态库添加到IOS插件
- javascript - 将 setTimeout 与递归函数一起使用
- ios - 领域将 pod 降级到以前的版本 (Swift)
- html - 悬停时的图像在悬停在另一张图像上后出现,但将两张图像彼此相邻