# 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 40
def age(n):
if n == 4:
return 40
return age(n+1)+2
print(age(1))
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/1.初识递归.py
46
Process finished with exit code 0
思路是获取头尾index,取中间索引值,与要查找的值进行判断,如果中间值大于要查找的值,那么就在列表的前半部分查找,如果中间值小于要查找的值,那么就在列表后半部分查找,折半查找时候,在更新要查找的切片的头尾索引值
def method(mLis,mVal):
minIndex = 0
maxIndex = len(mLis) -1
while True:
centerIndex = (minIndex + maxIndex) // 2
centerValue = mLis[centerIndex]
if centerValue < mVal:#右边
minIndex = centerIndex + 1
if centerValue > mVal:#左边
maxIndex = centerIndex - 1
if centerValue == mVal:
return centerIndex
print(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 = 24
if start <= end:
mid = (end-start) // 2 + start #mid = 12
if l[mid] > aim:
ret = find_2(l,aim,start,mid-1)
return ret
elif l[mid] < aim: #
ret = find_2(l,aim,mid+1,end) #find_2(l,58,13,24)
return ret
else:
return aim,mid
else:
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 choice
elif choice in menu and menu[choice]: #北京 in menu
borq = 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
北京
上海
选择 : b
Process 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 result
print(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