在元组中获取元素的索引
问题有关
这与许多类似的
但是,我不仅想检查它的存在,而且还想检查它的哪个元组,例如。索引。数据结构(来自第三方库)始终是元组的元组。 (它不能更深地嵌套)。
我想要作为返回值的是元素所在的元组索引。我知道我可以肯定地将其与loops一起入侵,但是有什么更好的方法可以获取索引吗?
This is related to many similar questions like
Check if element exists in tuple of tuples
However I do not only want to check it it exist but in which of the tuples it is in, eg. the index. The data structures (from 3rd party library) is always a tuple of tuples. (it can't be nested any deeper).
What I want as return value is the index of the tuple the element is located in. I know I can for sure hack this together with for loops but is there any nicer way to get the index?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 next 和枚举内置功能,可以具有获取索引的好方法。
您可以使用以下方式使用它:
使用它的示例:
you may use next and enumerate built-in functions to have a nice way of getting the index.
You may use it the following way:
Example of using it:
要重复嵌套元组中的元组元素,您可以轻松地使用单个循环进行管理。
索引
方法类似地查看每个元素,直到首先找到。我认为,只要适合您的需求,并且嵌套的元组也不大。For repeating tuple elements within in the nested tuple, you can easily manage with a single for loop.
The
index
method similarly looks into each element till reach first found. I think a for loop will work just fine as long it suits your needs and the nested tuple is not that large.这是循环的没有
的解决方案。外元组中包含查询元素
a
中包含查询元素的内部元组的索引Here's a solution without
for
loops. Indices of the inner tuples containing the queried element in the outer tuplea
are returned as a list