zoukankan      html  css  js  c++  java
  • 算法收集

    一、压栈

    menu = {
        '北京':{
            '海淀':{
                '五道口':{
                    'sogo':{},
                    '网易':{},
                    'google':{}
                },
                '中关村':{
                    '爱奇艺':{},
                    '汽车之家':{},
                    'youku':{},
                },
                '上地':{
                    '百度':{},
                },
            },
            '昌平':{
                '沙河':{
                    '汇德商厦老男孩':{},
                    '北航':{},
                },
                '天通苑':{},
                '回龙观':{},
            },
            '朝阳':{
                 '朝阳大妈':{}
            },
            '东城':{},
        },
        '上海':{
            '闵行':{
                "人民广场":{
                    '炸鸡店':{}
                }
            },
            '闸北':{
                '火车战':{
                    '携程':{}
                }
            },
            '浦东':{},
        },
        '山东':{
            '高丽':{}
        },
    }
    
    #            高级进阶版
    # last = [menu]   #上一层,起始层为当前菜单,将其作为列表存储起来
    # current = menu   #当前层,起始层为当前菜单
    # while True:   #循环执行
    #     for key in current:   #遍历菜单,是否在当前层
    #         print(key)   #展示菜单
    #     choice = input('请输入:').strip()   #输入选择内容
    #     if choice in current:    #开始选择,如果在当前层
    #         last.append(current)   #将当前层的所有选择追加进上一层列表
    #         current = current[choice]   #新当前层为旧当前层所选择的下一层,然后接着循环
    #     if choice == 'b':   #如果选择b
    #         # if last:    #数据越界进行选择,选择只在上一层列表中查找数据
    #             current = last[-1]   #当前层就是上一层最后一次存储的内容,将其取出并展示出来
    #             last.pop()     #将上一层最后取出的数据删除
    #     if choice == 'q':    #选择q
    #         break    #退出整个循环
    三级菜单
    fields='fld2|fld3|fld7|fld19'
    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}
    
    
    def select(data,fields):
        l = [data]
        field_lst = fields.split('|')
        result = {}
        while l:  # 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]
        return result
    
    print(select(data,fields))
    字典取值

     二、二分查找法

    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,17))
    二分查找法
  • 相关阅读:
    Python之图片格式转换
    pip依赖安装与记录
    Spectral Graph Theory的一些定理
    Beamer加中文
    Python之json
    Windows之建立C++开发环境
    Mysql分表教程
    null和空 not null
    yii 隐藏index.php的步骤
    yii泛域名
  • 原文地址:https://www.cnblogs.com/zjchao/p/7799062.html
Copyright © 2011-2022 走看看