• python -- day 11 考试题


     1.
      文件t1.txt里面的内容为:(6分)

      1,alex,22,13651054608,IT
      2,wusir,23,13304320533,Tearcher
      3,taibai,18,1333235322,IT
      利用文件操作,将其构造成如下数据类型。
      [{'id':'1','name':'alex','age':'22','phone':'13651054608',' 'job':'IT'},......]
    解:
      result = []
      f = open('t1.txt',mode = 'r',encouding='utf-8')
      for line in f:
        dic = {}
        line.split(',')
        dic['id'] = line[0]
        dic['nme'] = line[1]
        dic['age'] = line[2]
        dic['phone'] = line[3]
        dic['job'] = line[4].strip
      esult.append(dic)
      print(result)

    2.
       .

      解:    

      user_list = [
      {"name": "alex", "hobby": "抽烟"},
      {"name": "alex", "hobby": "喝酒"},
      {"name": "wusir", "hobby": "喊麦"},
      {"name": "wusir", "hobby": "街舞"},
      {"name": "alex", "hobby": "烫头"},
       {"name": "taibai", "hobby": "开车"},
       ]
      result = []
        for user in user_list:
          for new_user in result:
            if user['name'] = new_user['name']:
              new_user['hobby'].append(user['hobby'])
              break
        else:
          dic= {}
          dic['name']=user['name']
          dic['hobby'] = [user['hobby']]
          result.append(dic)
    print(result)

    3.  
      将大于66的值,放到一个key 中,将小于66的值,放到另一个key中
     
      解:
        li = [11,22,33,44,55,77,88,99,90]
        result = {}
        for row in li:
          if row > 66:
            result.setdefault('k1',[]).append(row)
          if row < 66:
            result.setdefault('k2',[]).append(row)
        print(result)
    4.
      实现一个计算器的加法
      如:content = input('请输入内容:')  
      如用户输入:5+8+7....(最少 输入两个数相加),将最后的计算结果添加到此字典中(替换None):
      dic={'最终计算结果':None}
      
      解:
        dic = {'最终计算结果':None}
        content = input("请输入内容").strip()
        lst = content.split(',')
        sum = 0
        for i in lst:
          sum = sum + int(i.strip())
        dic['最终计算结果'] = sum
        print(sum)
          


  • 相关阅读:
    【转】Android Web Server
    【转】Android Http Server
    一步一步教你150行代码实现简书滑动返回效果
    Android代码内存优化建议 OnTrimMemory
    Android应用Activity、Dialog、PopWindow、Toast窗口添加机制及源码分析
    Android 快速实现 ViewPager 滑动页卡切换(可用作整个 app上导航)
    阴影效果 ShadowLayout 布局实现(让控件实现立体效果)
    ViewDragHelper让你处理View拖动时,代码减半!
    Android淘宝电影日期选项卡的实现-tab 栏居中滚动
    Android开源项目分类汇总
  • 原文地址:https://www.cnblogs.com/wf123/p/9325688.html
Copyright © 2020-2023  润新知