python超多层序列找到指定值的路径关系(包括字典列表元组混合的情况)

背景

平时遇到的序列最多套个三层就差不多了,但是最近在爬金山词霸的时候
在这里插入图片描述
如上图下面这些例句,后面几个指定意思的例句要爬出来就得找script标签里的东西。但是一找就懵逼了

在这里插入图片描述
字典套字典套字典套字典套字典套列表套字典套…数不过来有多少层了一开始想网上随便找个别人写好的找路径代码,但是都是只能单找字典,我这个情况是字典和列表混合嵌套啊。还是自己写一个找路径的代码模块吧emm。也方便以后和我一样找不到能直接拿来用的代码的人

代码效果展示

输出
在这里插入图片描述
代码
在这里插入图片描述

在这里插入图片描述

具体代码

主要是用递归,然后内部会对不同数据类型分类讨论
功能:函数find_path可以返回一个列表,包含所有符合的路径
第一个参数是要进行查找操作的序列;第二个是模式,0是找字典的键名,1是找值字典或者列表元组都行;第三个是目标量;第四个在使用时不用管不用传值,它是为了在递归的时候传入当前正在查找的路径位置用的。

def find_path(iterable, mode, target, current_path=''):
    '''
    
	By
	----------
	CSDN 虚数魔方

    Parameters
    ----------
    iterable : dict or list or tuple
        The object iterable.
    mode : int
        You can use 0 or 1. 0 means look up the key name of dict and i means look up the value
    target : arbitrary type
        Your target for look up.
    current_path : string, optional
        This parameter is used to run the function and you don't need to give it a value when you use it. The default is ''.

    Returns
    -------
    path : list
        This list contains all eligible paths.

    '''

    path = []
    
    if type(iterable) == type({
    
    }):#如果为字典类型
        if mode == 0:#查找键名模式
            for key in iterable.keys():
                c_p = current_path + " → <dict>['%s']" % key
                if key == target:
                    path.append(c_p)
                if type(iterable[key]) in [type({
    
    }),type(()),type([])]:#如果键值是序列就继续向下找
                    path += find_path(iterable[key], mode, target, c_p)
        elif mode == 1:
            for key in iterable.keys():
                c_p = current_path + " → <dict>['%s']"%key
                if iterable[key] == target:
                    path.append(c_p)
                if type(iterable[key]) in [type({
    
    }),type(()),type([])]:#如果键值是序列就继续向下找
                    path += find_path(iterable[key], mode, target, c_p)
                    
                    
    elif type(iterable) == type(()):#如果为元组类型
        if mode == 0:#元组没有键名 直接往下找
            for i,v in enumerate(iterable): 
                if type(v) in [type({
    
    }),type(()),type([])]:#如果键值是序列
                    c_p = current_path + " → <tuple>[%s]" % i
                    path += find_path(v, mode, target, c_p)
        elif mode == 1:
            for i,v in enumerate(iterable):
                c_p = current_path + " → <tuple>[%s]" % i
                if v == target:
                    path.append(c_p + ':\n%s'%v)
                if type(v) in [type({
    
    }),type(()),type([])]:#如果键值是序列就继续向下找
                    path += find_path(v, mode, target, c_p)
    elif type(iterable) == type([]):#如果为列表类型
        if mode == 0:#列表没有键名 直接往下找
            for i,v in enumerate(iterable): 
                if type(v) in [type({
    
    }),type(()),type([])]:#如果键值是序列
                    c_p = current_path + " → <list>[%s]" % i
                    path += find_path(v, mode, target, c_p)
        elif mode == 1:
            for i,v in enumerate(iterable):
                c_p = current_path + " → <list>[%s]" % i
                if v == target:
                    path.append(c_p + ':\n%s'%v)
                if type(v) in [type({
    
    }),type(()),type([])]:#如果键值是序列就继续向下找
                    path += find_path(v, mode, target, c_p)
               
    return path

//希望这能帮到你

猜你喜欢

转载自blog.csdn.net/qq_47110957/article/details/106982333