如何在嵌套列表中查找给定的元素? [英] How to find a given element in nested lists?

查看:233
本文介绍了如何在嵌套列表中查找给定的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的迭代解决方案:

  def exists(key,arg):
如果不是arg:
返回False
else:
用于arg中的元素:
if isinstance(element,list):
用于元素中的元素:
if i == key :
return True
elif element == key:
return True
return False
print(exists(f,[a,[h ,e,j],[t,e,s,c,o]]))


我的尝试:

  def exists(key,arg):
如果不是arg:// base case
return False
elif arg [0] == key://如果我们从第一次试验中发现密钥
返回True
else:
return(exists(arg [0:],key))

这不起作用;它不应该,因为没有停止。此外,它没有考虑列表清单;我不知道该怎么做。



任何回答,评论等都会被赞赏

解决方案

如果我们考虑这些情况:


  • my_list 为空:找不到密钥

  • my_list 不是列表:找不到密钥

  • my_list 是一个非空列表(两种情况):


    • my_list [0] 是关键:它被发现

    • 否则,请在 my_list [0] my_list [1:]




    代码将会是:

      def exists(key,my_list): 
    如果不是isinstance(my_list,list)或者不是my_list:
    return False

    return(my_list [0] == key
    或exists(key,my_list [ 0])
    或存在(key,my_list [1:]))

    甚至

      def exists(key,my_list):
    返回(isinstance(my_list,list)
    和len(my_list)> (key,my_list [0])
    和(my_list [0] == key
    或exists(key,my_list [1:])))


    This is my iterative solution:

    def exists(key, arg):
        if not arg:
            return False
        else:
            for element in arg:
                if  isinstance(element,list):
                    for i in element:
                        if i==key:
                            return True
                elif element==key:
                    return True
        return False
    print(exists("f", ["a", ["h", "e", "j"], ["t", "e", "s", "c", "o"]]))
    

    However, my L.A. wants a double recursive function to solve this.

    my attempt:

    def exists(key, arg):
        if not arg: //base case
            return False
        elif arg[0]==key: //if we find the key from the first trial
            return True
        else:
            return (exists(arg[0:],key))
    

    This doesn't work; it shouldn't, because there is no stop. Also, it does not account for lists of lists; I don't know how to do that.

    Any answer, comment, etc. is appreciated

    解决方案

    If we consider these cases:

    • my_list is empty: the key isn't found
    • my_list is not a list: the key isn't found
    • my_list is a non-empty list (two cases):
      • my_list[0] is the key: it was found
      • otherwise, look for the key in both my_list[0] and my_list[1:]

    the code would be

    def exists(key, my_list):
        if not isinstance(my_list, list) or not my_list:
            return False
    
        return (my_list[0] == key
                or exists(key, my_list[0]) 
                or exists(key, my_list[1:]))
    

    or even

    def exists(key, my_list):
        return (isinstance(my_list, list)
                and len(my_list) > 0 
                and (my_list[0] == key
                     or exists(key, my_list[0])
                     or exists(key, my_list[1:])))
    

    这篇关于如何在嵌套列表中查找给定的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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