本文主要是介绍python基础-递归、二分法查找(for\递归)、三级菜单、压栈思想,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
-
-
-
- 递归方法
- 二分法查找
- 通过for循环实现
- 通过递归实现
- 递归应用–三级菜单
- 压栈
-
-
递归方法
# age(1) n = 1 age(2)+2
# age(2) n = 2 age(3)+2
# age(3) n = 3 age(4)+2
# age(4) n = 4 40def age(n):if n == 4:return 40return age(n+1)+2print(age(1))
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/1.初识递归.py
46Process finished with exit code 0
二分法查找
通过for循环实现
思路是获取头尾index,取中间索引值,与要查找的值进行判断,如果中间值大于要查找的值,那么就在列表的前半部分查找,如果中间值小于要查找的值,那么就在列表后半部分查找,折半查找时候,在更新要查找的切片的头尾索引值
def method(mLis,mVal):minIndex = 0maxIndex = len(mLis) -1while True:centerIndex = (minIndex + maxIndex) // 2centerValue = mLis[centerIndex]if centerValue < mVal:#右边minIndex = centerIndex + 1if centerValue > mVal:#左边maxIndex = centerIndex - 1if centerValue == mVal:return centerIndexprint(method(l,18))
输出如下:6
通过递归实现
def find_2(l,aim,start=0,end=None): #[2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]if end == None:end = len(l) - 1 #start = 0,end = 24if start <= end:mid = (end-start) // 2 + start #mid = 12if l[mid] > aim:ret = find_2(l,aim,start,mid-1)return retelif l[mid] < aim: #ret = find_2(l,aim,mid+1,end) #find_2(l,58,13,24)return retelse:return aim,midelse:print('找不到这个值')
l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]
print(find_2(l,22))
输出如下:
(22, 7)
递归应用–三级菜单
menu = {'北京': {'海淀': {'五道口': {'soho': {},'网易': {},}},'昌平': {'沙河': {'北航': {},},'天通苑': {}}},'上海': {'闵行': {"人民广场": {'炸鸡店': {}}}}
}def menu_3(menu):while True:for key in menu:print(key) #北京上海山东choice = input('选择 : ') #北京if choice == 'q' or choice == 'b':return choiceelif choice in menu and menu[choice]: #北京 in menuborq = menu_3(menu[choice])if borq == 'q':return 'q'menu_3(menu)
测试如下:
E:\python\python_sdk\python.exe E:/python/py_pro/4.三级菜单.py
北京
上海
选择 : 北京
海淀
昌平
选择 : 海淀
五道口
选择 : 五道口
soho
网易
选择 : b
五道口
选择 : b
海淀
昌平
选择 : b
北京
上海
选择 : bProcess finished with exit code 0
压栈
从该数据结构中返回由指定的字段和对应的值组成的字典,如果指定字段不存在就跳过该字段
这道题使用了压栈的做法
通过data = l.pop()取出最后一个栈内元素赋值变量,意味着将l清空了,如果data里面的键在列表field_lst里面,就添加进一个新的result 字典里面,如果data的键对应的值是字典,那么就添加进来到l列表,例如{ “fld1”:1,”fld2”:2}
循环完毕后,列表里面就包含了[{‘fld1’: 1, ‘fld2’: 2}, {‘fld3’: 0, ‘fld5’: 0.4}, {‘key’: {‘fld19’: 1}}]
然后开始一个个pop获取键值对
fields = "fld2|fld5|fld6|fld19|fld7|fld46"
data={"time":"2016-08-05T13:13:05","some_id":"ID1234","grp1":{ "fld1":1,"fld2":2},"xxx2":{ "fld3":0,"fld5":0.4},"fld6":{"key":{ "fld19":1}},"fld7":7,"fld46":8}
#计算机思想:压栈,Alex
def select(data,fields):l = [data]field_lst = fields.split('|')result = {}while l:data = l.pop() #data = { "fld1":1,"fld2":2}for key in data:if type(data[key]) == dict:l.append(data[key]) # l = [{ "fld1":1,"fld2":2},{ "fld3":0,"fld5":0.4},{"key":{ "fld19":1}}]elif key in field_lst:result[key] = data[key]print("-----",l)return resultprint(select(data,fields))
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/python.py
----- [{'fld1': 1, 'fld2': 2}, {'fld3': 0, 'fld5': 0.4}, {'key': {'fld19': 1}}]
----- [{'fld1': 1, 'fld2': 2}, {'fld3': 0, 'fld5': 0.4}, {'fld19': 1}]
----- [{'fld1': 1, 'fld2': 2}, {'fld3': 0, 'fld5': 0.4}]
----- [{'fld1': 1, 'fld2': 2}]
----- []
{'fld7': 7, 'fld46': 8, 'fld19': 1, 'fld5': 0.4, 'fld2': 2}Process finished with exit code 0
这篇关于python基础-递归、二分法查找(for\递归)、三级菜单、压栈思想的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!