Python infinite traversal for data acquisition in multidimensional nested dictionaries, lists, and tuples of JSON

Source: Internet
Author: User

Interface returns the possibility of nested lists in JSON data, and nested dictionaries within a list,

In the process of interface automation, it is necessary to obtain the corresponding value directly through a key value, so we have the following function

Not much to say, on the code:

#!/usr/bin/python#Coding:utf-8"""@author: bingo.he @file: get_target_value.py @time: 2017/12/22"""defGet_target_value (Key, DIC, tmp_list):""":p Aram Key: Target key value:p Aram Dic:json data:p Aram Tmp_list: Used to store acquired data: Return:list"""    if  notIsinstance (DIC, Dict)or  notisinstance (Tmp_list, list): # format check for incoming data          return 'Argv[1] Not a dict or argv[-1] not a list'
ifKeyinchDic.keys (): Tmp_list.append (Dic[key])#incoming data is stored tmp_list Else: forValueinchDic.values ():#traversal of the value of an incoming data if it does not conform ifisinstance (value, dict): Get_target_value (key, Value, Tmp_list)#The value of the incoming data is a dictionary, which calls itself directly elifisinstance (value, (list, tuple)): _get_value (key, Value, Tmp_list)#if the value of the incoming data is a list or a tuple, call _get_value returntmp_listdef_get_value (Key, Val, tmp_list): forVal_inchVal:ifisinstance (Val_, Dict): Get_target_value (Key, Val_, tmp_list)#if the value of the incoming data is a dictionary, call Get_target_value elifisinstance (Val_, (list, tuple)): _get_value (Key, Val_, tmp_list)#if the value of the incoming data is a list or a tuple, call itself

A simple test of a function that can get the desired value from a complex set of multiple nested data

Holes encountered when writing this function:

Initially, the blogger stores the temporary list of retrieved data into the parameters, so that the call can pass a parameter less.

def Get_target (A, b=[]):    b.append (a)    print(b)

Get_target (1)

However, it turns out that there is no problem with a single call, but when multiple calls are made, the value of the last call is returned.

Could be a bug in the Python function itself

And then we looked at a lot of information. The default value of the parameter is only initialized once for the function declaration, and then no longer initialized

def foo (a=[]) and foo (a=[]): The former is the default value of the parameter, the latter is keyword arguments

The following code definition and invocation is also a slight difference between

def foo (*args, * *Kargs)    :pass    foo (*args, **kargs)

Python infinite traversal for data acquisition in multidimensional nested dictionaries, lists, and tuples of JSON

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.