在元组中获取元素的索引

发布于 2025-02-01 02:29:21 字数 169 浏览 1 评论 0原文

问题有关

这与许多类似的

但是,我不仅想检查它的存在,而且还想检查它的哪个元组,例如。索引。数据结构(来自第三方库)始终是元组的元组。 (它不能更深地嵌套)。

我想要作为返回值的是元素所在的元组索引。我知道我可以肯定地将其与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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瀟灑尐姊 2025-02-08 02:29:21

您可以使用 next 枚举内置功能,可以具有获取索引的好方法。
您可以使用以下方式使用它:

def get_index_of_containing_tuple(lst: tuple, item):
    try:
        return next(ind for ind, tup in enumerate(lst) if item in tup)
    except Exception:
        return -1

使用它的示例:

a = (('a', 'b'), ('c', 'd'))
get_index_of_containing_tuple(a, 'a')  # returns 0
get_index_of_containing_tuple(a, 'c')  # returns 1
get_index_of_containing_tuple(a, 'd')  # returns 1
get_index_of_containing_tuple(a, 123)  # returns -1

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:

def get_index_of_containing_tuple(lst: tuple, item):
    try:
        return next(ind for ind, tup in enumerate(lst) if item in tup)
    except Exception:
        return -1

Example of using it:

a = (('a', 'b'), ('c', 'd'))
get_index_of_containing_tuple(a, 'a')  # returns 0
get_index_of_containing_tuple(a, 'c')  # returns 1
get_index_of_containing_tuple(a, 'd')  # returns 1
get_index_of_containing_tuple(a, 123)  # returns -1
策马西风 2025-02-08 02:29:21

要重复嵌套元组中的元组元素,您可以轻松地使用单个循环进行管理。

example = (('apple','orange'),('orange','banana'),('banana','mango'))

def element_index(iterable, key = None):
    index = set()
    index_add = index.add
    
    for ix, element in enumerate(iterable):
        if key in element:
            index_add(ix)
            
    return index

nth_tuple(example, key = 'orange')

>> {0, 1}

索引方法类似地查看每个元素,直到首先找到。我认为,只要适合您的需求,并且嵌套的元组也不大。

For repeating tuple elements within in the nested tuple, you can easily manage with a single for loop.

example = (('apple','orange'),('orange','banana'),('banana','mango'))

def element_index(iterable, key = None):
    index = set()
    index_add = index.add
    
    for ix, element in enumerate(iterable):
        if key in element:
            index_add(ix)
            
    return index

nth_tuple(example, key = 'orange')

>> {0, 1}

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.

离线来电— 2025-02-08 02:29:21

这是循环的没有的解决方案。外元组中包含查询元素a中包含查询元素的内部元组的索引

list(filter(lambda x: x != -1, map(lambda enum: enum[0] if 'queried_element' in enum[1] else -1, enumerate(a)))

Here's a solution without for loops. Indices of the inner tuples containing the queried element in the outer tuple a are returned as a list

list(filter(lambda x: x != -1, map(lambda enum: enum[0] if 'queried_element' in enum[1] else -1, enumerate(a)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文