1. 通过 sql 语句筛选出符合条件的数据

condition_dict = {'name': 1, 'id': 0, 'age': 2, 'phone': 3, 'job': 4}


def get_data():
'''
    获取文件中的数据
    :return: 生成器
    '''
    with open('staff', encoding='utf-8') as f:
        for i, l in enumerate(f):
            if i > 0:
                yield l.strip().split(',')


def condition_fn(c):
'''
    根据条件进行筛选
    :param c: 条件的字符串 -> age > 10
    :return: 符合条件的列表
    '''
    if '>' in c:
        col, val = c.split('>')
        g = get_data()
        for i in g:
            if int(i[condition_dict[col]]) > int(val):
                yield i
    elif '<' in c:
        col, val = c.split('<')
        g = get_data()
        for i in g:
            if int(i[condition_dict[col]]) < int(val):
                yield i
    elif '=' in c:
        col, val = c.split('=')
        g = get_data()
        for i in g:
            if i[condition_dict[col]] == val:
                yield i


def show_data(view_list, field):
 '''
    展示符合条件的员工数据
    :param view_list: 符合条件的员工信息生成器
    :param field: name,age
    :return:
    '''
    if '*' in field:
        for i in view_list:
            print(i)
    else:
        f_name = field.split('select')
        f_name_arr = f_name[1].strip().split(',')
        for i in view_list:
            for n in f_name_arr:
                print(i[condition_dict[n]], end=' ')
            print('')


ret = 'select name,age where age<23'
field, condition = ret.split('where')
view_list = condition_fn(condition.strip())
show_data(view_list, field.strip())

# staff

id,name,age,phone,job
1,Alex,22,13651054608,IT
2,Egon,23,13304320533,Tearcher
3,nezha,25,1333235322,IT

2. 三级菜单

menu = {
    '北京': {
        '海淀': {
            '五道口': {
                'soho': {},
                '网易': {},
                'google': {}
            },
            '中关村': {
                '爱奇艺': {},
                '汽车之家': {},
                'youku': {},
            },
            '上地': {
                '百度': {},
            },
        },
        '昌平': {
            '沙河': {
                '老男孩': {},
                '北航': {},
            },
            '天通苑': {},
            '回龙观': {},
        },
        '朝阳': {},
        '东城': {},
    },
    '上海': {
        '闵行': {
            "人民广场": {
                '炸鸡店': {}
            }
        },
        '闸北': {
            '火车战': {
                '携程': {}
            }
        },
        '浦东': {},
    },
    '山东': {},
}


def threeLM(dic):
    while True:
        for k in dic: print(k)
        content = input('>>> ').strip()
        if content == 'q':
            break
        elif content in dic:
            print(dic[content])
            threeLM(dic[content])
        else:
            threeLM(menu)


threeLM(menu)

3. 计算器

import re


def dealwith(express):
# 对表达式中的符号进行处理 +- 替换成 - ,-- 替换成 +
    express = express.replace('+-', '-')
    express = express.replace('--', '+')
    return express


def cal_exp_son(exp_son):
# 用来计算原子型的表达式 两个数之间的乘除法
    if '/' in exp_son:
        a, b = exp_son.split('/')
        return str(float(a) / float(b))
    else:
        a, b = exp_son.split('*')
        return str(float(a) * float(b))


def cal_express_no_breacket(exp):
 # 计算没有括号的表达式
    # exp是没有经过处理的最内层带括号的表达式
    exp = exp.strip('()')
    while True:
        ret = re.search(r'\d+\.?\d*[*/]-?\d+\.?\d*', exp)  # 匹配第一个乘除
        if ret:  # 判断表达式中是否还有乘除
            exp_son = ret.group()
            print(exp_son)
            ret = cal_exp_son(exp_son)
            exp = exp.replace(exp_son, ret)
            exp = dealwith(exp)
        else:
            ret = re.findall(r'-?\d+\.?\d*', exp)
            sum = 0
            for i in ret:
                sum += float(i)
            return str(sum)


def remove_bracket(new_express):
# 提取括号里面没有其他括号的表达式
    while True:
        ret = re.search(r'\([^()]+\)', new_express)
        if ret:
            express_no_breacket = ret.group()
            print('匹配到内部不再有括号的值:', express_no_breacket)
            ret = cal_express_no_breacket(express_no_breacket)
            print(new_express, express_no_breacket, ret)
            new_express = new_express.replace(express_no_breacket, ret)
            new_express = dealwith(new_express)
            print(new_express)
        else:
            print('表达式中没有括号了:', new_express)
            ret = cal_express_no_breacket(new_express)
            print(ret)
            break


express = '1 - 2 * ( ( 6 0 -3 0  +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'
new_express = express.replace(' ', '')
remove_bracket(new_express)