Your browser does not support the Canvas element.

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)
      


posted @ 2018-07-17 20:27  一根小菜  阅读(133)  评论(0编辑  收藏  举报
Your browser does not support the Canvas element.