zoukankan      html  css  js  c++  java
  • 第二模块第一章作业

    知识:以后还要再回顾,到时再写
    str.find()实际返回的是索引值,如果需对查找字符串作条件判断,第一个字符串刚好是索引0,也就是布尔值False,而找不到返回的是-1或者索引非0,则返回true。
         ---》用以下方法可对查找字符串作判断,根据.find 找不到字符串返回-1的特点,把返回的索引值+1再判断即可,找不到返回-1,+1后刚好变成0,为false,而找得到就算+1也是True


    #! /usr/bin/env python
    #.-*- coding:utf-8 -*-
    #.__author__.='J"

    # 删除信息和第4题只操作一遍,所以不计数n了


    def file_to_D(): # 把文件内容变成以索引为key的字典,每个索引指向一条个人信息
    global D
    D={}
    f = open('E:test.txt','r')
    d = f.readlines()
    for i in d:
    i = i.split(',')
    i[0] = int(i[0])
    D[i[0]] = i
    print('总表',D)
    # print(len(D))

    def save_to_file():
    f = open('E:test.txt','w')
    for i in D.values():
    i = ','.join(i)
    f.write(i)
    f.flush()
    f.close()


    def question1_1(s):
    l1 = []
    n = 0
    for i in D.values():
    if int(i[2]) > s:
    l1.append((i[1],i[2]))
    n += 1
    print(l1)
    print('记录: ',n)


    def question1_2(s):
    global p
    p = []
    n = 0
    for i in D.values():
    if i[4] == s:
    p.append(i)
    n += 1
    print(p)
    print('记录: ', n)

    def question1_3(s):
    l3 = []
    n = 0
    for i in D.values():
    if i[5].find(s)+1: # 为什么这里找到2013返回的是false,百度上都说返回索引值---实际返回的是索引值,因为2013在日期字符串排第一位,也即是索引位为0,返回布尔值False。而如果搜索非首位的关键字,则这个代码是无效的,因为找不到返回的是-1,也就是true。---》根据这个特点,把返回值+1再判断即可,找不到返回-1,+1后刚好变成0,为false,而找得到就算+1也是True
    l3.append(i)
    n += 1
    print(l3)
    print('记录: ', n)


    def question2(name,age,phone,dept,birth):
    global n2
    global staff_id
    n2 = 1
    staff_id = next(a) # next 冻结生成器,不是冻结函数,yield才会
    birth = str(birth)
    staff_id = str(staff_id)
    D[phone] = {staff_id,name,age,phone,dept,birth}
    print(D)
    save_to_file() # 实际没写进去,为什么
    print('记录 ',n2)

    def question3(id):
    if 0 < id < len(D):
    D.pop(id)
    for i in D.values():
    i[0] = str(i[0])
    else:
    print('输错了')


    print(D)
    save_to_file()

    def question4(name='handsome', age = '666', dept= 'super man', birth = '1'): # 默认了这几个参数不让输入
    for index,i in D.items():
    if dept == 'IT':
    i[4] = 'market'
    print('修改部门后的:',i)
    if name == 'Alex Li':
    i[2] = '25'
    print('修改年龄后的:',i)
    D[index] = i
    print('总表:', D)
    save_to_file()

    file_to_D()
    a = (i for i in range(1, 1000000001))


    syntax = input('syntax:>>')

    def judge_str(syntax):
    l = syntax.split(' ') # 把命令变成一个个字符串的列表
    print('命令: ',l)
    for i in l:
    i.strip()
    # print(l)
    # if l[0] == 'find' and l[1] == 'name' # 1_1 name,age这里判断不了,先不做
    if l[0] == 'find': # 这里判断却不用对s进行eval(s)
    if l[1] == '*' and l[2] == 'from' and l[3] == 'staff_table' and l[4] == 'where' and l[5] == 'dept' and l[6] == '=' and len(l) == 8:
    s = l[7]
    # print(s)
    s = eval(s) # 为什么加了这句后S就能识别到了
    if s in ['IT',"Sales",'HR','Marketing','Operation','Administration']:
    question1_2(s) # 为什么question1_2(s)返回的是一个空列表,函数读不到s变量字符串的值?
    else:
    print('输入错误') #第一题第二问

    elif l[1] == 'name,age' and l[2] == 'from' and l[3] == 'staff_table' and l[4] == 'where' and l[5] == 'age' and l[6] == '>' and len(l) == 8:
    s = l[7]
    s = eval(s)
    print(s,type(s))
    question1_1(s) # 第一题第一问

    elif l[1] == '*' and l[2] == 'from' and l[3] == 'staff_table' and l[4] == 'where' and l[5] == 'enroll_date' and l[6] == 'like' and len(l) == 8:
    s = l[7]
    # s = eval(s)
    print(s, type(s))
    question1_3(s)
          # 第3题第3问
    elif l[0] == 'add': # 第二题
    if l[1] == 'staff_table' and len(l) == 7:
    question2(l[2],str(l[3]),str(l[4]),l[5],l[6])
    elif l[0] == 'del': # 第三题
    if l[1] == 'from' and l[2] == 'staff' and l[3] == 'where' and l[4] == 'id' and l[5] == '=' and len(l) == 7:
    s = int(l[6])
    question3(s)
    # 第四题不用写了,复制粘贴,一样套路

    judge_str(syntax) # 实现根据任意部门查找个人信息find * from staff_table where dept = "IT"





  • 相关阅读:
    SpringBoot实战项目(十)--用户修改功能之后台存储
    SpringBoot实战项目(九)--用户修改功能之显示用户对应角色
    SpringBoot实战项目(八)--用户修改功能之显示用户信息
    SpringBoot实战项目(七)--用户新增功能之后台存储
    SpringBoot实战项目(六)--用户新增功能之验证处理
    SpringBoot实战项目(五)--用户新增功能之页面构建
    jQuery 杂项方法大全
    jQuery AJAX方法详谈
    jQuery 遍历方法大全
    PHP 数组函数大全
  • 原文地址:https://www.cnblogs.com/jackfree/p/9671657.html
Copyright © 2011-2022 走看看