在多维列表中搜索元素并返回子列表编号? [英] Searching for an element inside a multi-dimensional list and to return the sub-list number?

查看:37
本文介绍了在多维列表中搜索元素并返回子列表编号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前我有一个关于查找元素是否存在于嵌套列表中的问题,并在下面的链接中得到了响应.

Previously I had a question on finding whether an element exists in a nested list or not and got the response in the below link.

搜索元素内部多维列表不起作用.任何线索

想知道该函数是否也可以发送一个值来说明元素存在于哪个子列表中?下面是说明元素是否退出的代码:

Was wondering if the function could as well send out a value which says in which sub-list does the element exists ? Below is the code which says if the element exits or not:

def in_nested_list(item, x):
    if not isinstance(x, list):
        return x == item
    else:
        return any(in_nested_list(item, ix) for ix in x)

list1 = []
list1.append([['red'], ['blue'], ['bla']])

print list1

list1.append([['hello'], ['blue'], ['bla']])
print list1

if in_nested_list('hello', list1):
    print "Found Hello"
else:
    print "Not Found Hello"

想知道如何更改该函数以同时说明元素在哪个子列表中退出,以便我可以根据特定子列表附加更多数据.

Was wondering how could I change that function to also say in which sub-list does the element exits so that I can append further data based onto particular sub list.

一个.从上面的例子中,if 条件将打印出 Hello 已找到.但是,如果它还返回或保存一个输出变量,例如 which sublist_no 即子列表 1 在上述情况下,以便我可以附加更多变量.

a. From the above example, the if condition will print that Hello is found. But if it also returns or saves an output variable like which sublist_no i.e. sub-list 1 in the above case, so that I can append further variable.

例如:现在输出返回TRUE,并且子列表编号也为1,以便我在调用该函数后可以执行以下过程.

Eg: Now the output return TRUE and also the sub-list number as 1, so that I can do the following process after calling the function.

list1[sublist_no].append(['Bug'])

假设我搜索元素 red,输出将为 TRUE,sublist_no 为 0,以便我可以将更多变量附加到第一个列表.list1[sublist_no].append('[Rasberry]') # 即进入第一个子列表.

Say if I search for the element red, the output will be TRUE with sublist_no as 0, so that I can append some more variable to the 1st list. list1[sublist_no].append('[Rasberry]') # i.e. onto the first sub-list.

关于上述查询的任何线索?请留下您的评论...

Any clue on the above query ? Kindly drop in your comment...

推荐答案

我利用了修改你的功能.

I took leverage to modify your function.

def in_nested(item, container):
    if not isinstance(container, list):
        raise TypeError
    for elem in container:
        if isinstance(elem, list):
            found = in_nested(item, elem)
            if not found:
                pass
            elif isinstance(found, bool):
                # Item is present in this list
                return elem
                # List containing item is present in this container
                # Comment above return to use it.
                return container
            else:
                # Return the deepest list and not the most outer list containing it.
                return found
        else:
            return item == elem


_input = [[['red'], ['blue'], ['bla']], [['hello'], ['blue'], ['bla']]]
out = in_nested('hello', _input)
if out:
    print "Found Hello", out
else:
    print "Not Found Hello"

out.append('new')
print 'Added new to original input', _input

out = in_nested('Hello', _input)  # hello with capital H
if out:
    print "Found Hello", out
else:
    print "Not Found Hello"

# Prints:
# Found Hello ['hello']
# Added new to original input [[['red'], ['blue'], ['bla']], [['hello', 'new'], ['blue'], ['bla']]]
# Not Found Hello

注意:它为您提供目标列表本身,如果项目存在否则为无.您不必再次找到该列表.只需附加到结果.

Note: It gives you the target list itself, if item is present else None. You don't have to locate the list again. Just append to the result.

这篇关于在多维列表中搜索元素并返回子列表编号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆