In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to use python recursive function". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
First of all, functions are functions, recursive functions are still functions, and we should not mistake them for many kinds. It's just that recursive functions are different from other functions, so I gave them a separate name to facilitate our communication. As soon as you said recursive functions, I understood that this function will call itself when it is executed, that's all.
Let's start with my problem. My program calls an API and returns a dictionary. It's uncomfortable to see that the information I need is in the dictionary, but the distribution is not fixed. For example, the following dictionary:
maze_dict = {
'machine':'apple',
'name':u'l',
'bus':{
'no':1,
'direction':'east',
'west':{
'where':'beijing',
'date':'today',
'python':'python1'
}
},
'python':'python2',
'pythongroup':{
'group':{
'group':{
'group':{
'python':'python3'
}
}
}
}
}
Suppose that the information I want to extract is the value part of the key-value pair with python as the keyword, but I don't know where the python keyword is in advance. I can't go from the outermost layer to the inner layer by keyword, because the path is uncertain. Here, we're going to use recursive functions to advance layer by layer.
def find_python(info,lst):
if not isinstance(info,dict):
return
for k,v in info.items():
if k == 'python':
lst.append(v)
elif isinstance(v,dict):
find_python(v,lst)
if __name__ == '__main__':
lst = []
find_python(maze_dict,lst)
print lst
The definition of the find_python function is not complicated. When the value part of a key-value pair is a dictionary, it calls itself again and goes to the next level to find the python keyword.
The difficulty in understanding recursive functions lies in that every function call is executed from the beginning, but the parameters passed in by the function are changing. Secondly, when entering the next layer of functions, this layer of functions does not end, but when the next layer of functions ends, they continue to execute them. Just like the dream space, the dreams progress layer by layer, but there will also be an exit process. It must be that the dream of the next layer ends, and the dreamer of the current layer wakes up. Then, he continued to exit, and the people in the previous layer of dreams woke up.
"Python recursive function how to use" the content of the introduction here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.