作业

# 作业:
# 1、文件内容如下,标题为:姓名,性别,年纪,薪资
#     egon male 18 3000
#     alex male 38 30000
#     wupeiqi female 28 20000
#     yuanhao female 28 10000
#
# 要求:
# 从文件中取出每一条记录放入列表中,
# 列表的每个元素都是{'name':'egon','sex':'male','age':18,'salary':3000}的形式
# with open('a.txt',mode='rt',encoding='utf-8')as f:
#  items = (line.split() for line in f)
#  res = [{'name': name, 'gender': gender, 'age': age, 'salary': salary}for name, gender, age, salary in items]
#  print(res)
#
# 2 根据1得到的列表,取出薪资最高的人的信息
# print(max(res,key= lambda k:k["salary"]))
# 3 根据1得到的列表,取出最年轻的人的信息
# print(min(res,key= lambda k:k["age"]))
# 4、将names=['egon','alex_sb','wupeiqi','yuanhao']中的名字全部变大写
# names=['egon','alex_sb','wupeiqi','yuanhao']
# res = [name.upper for name in names]


#
# 5、将names=['egon','alex_sb','wupeiqi','yuanhao']中以sb结尾的名字过滤掉,然后保存剩下的名字长度
# res1=[name for name in names if not name.endswith('sb')]
# print(res1)
#
# 6、求文件a.txt中最长的行的长度(长度按字符个数算,需要使用max函数)
# with open('a.txt', mode='rt', encoding='utf-8')as f:

# res1 = (sum(len(res) for line in f))
# print(res1)
# 7、求文件a.txt中总共包含的字符个数?思考为何在第一次之后的n次sum求和得到的结果为0?(需要使用sum函数)
# with open('a.txt', mode='rt', encoding='utf-8')as f:
#  res = (sum(len(line)for line in f))
#  print(res)

#
# 8、思考题
#
# with open('a.txt',) as f:
#     g=(len(line) for line in f)
# print(sum(g)) #为何报错?

# 文件之外打印的g,文件已经关闭了
# 9、文件shopping.txt内容如下
#
# mac,20000,3
# lenovo,3000,10
# tesla,1000000,10
# chicken,200,1
# 求总共花了多少钱?
# with open('shopping', 'rt', encoding='utf-8')as f:
#  items = (line.strip().split(',') for line in f)
#  res = [{"name": name, "price": price, "count": count} for name, price, count in items]
#  for line in res:


# 打印出所有商品的信息,格式为[{'name':'xxx','price':333,'count':3},...]
# with open('shopping','rt',encoding='utf-8')as f:
#  items = (line.split(',') for line in f)
#  res = [{"name":name,"price":price,"count":count}for name,price,count in items]
#  print(res)
# 求单价大于10000的商品信息,格式同上
# with open('shopping', 'rt', encoding='utf-8')as f:
#  items = (line.strip().split(',') for line in f)
#  res = [{"name": name, "price": price, "count": count} for name, price, count in items]
#
#  for line in res:
#     line = int(line["price"])
#     if line > 10000:
#        print(line)


# 10、文件内容如下,标题为:姓名,性别,年纪,薪资
# egon male 18 3000
# alex male 38 30000
# wupeiqi female 28 20000
# yuanhao female 28 10000
#
# 要求:
# 从文件中取出每一条记录放入列表中,
# 列表的每个元素都是{'name':'egon','sex':'male','age':18,'salary':3000}的形式
# with open('a.txt', mode='rt', encoding='utf-8')as f:
#  item = (line.strip().split() for line in f)
#  res = [{"name": name, "gender": gender, "age": age, "price": price} for name, gender, age, price in item]
   # print(res)
   # 根据1得到的列表,取出所有人的薪资之和

   # print(sum(int(line['price']) for line in res))
   # 根据1得到的列表,取出所有的男人的名字
   #  res1 = (line.strip for line in f)
   #  res2 = [line for line in res if line["gender"] == 'male']
   #  print(res2)
   # 根据1得到的列表,将每个人的信息中的名字映射成首字母大写的形式

   # res1 = (line.split() for line in f)
   # res2 = [line['name'].title() for line in res]
   # print(res2)
   # 根据1得到的列表,过滤掉名字以a开头的人的信息
   # res1 = (line.split() for line in f)
   # res2 = [line for line in res if not line["name"].startswith('a')]
   # print(res2)
# 11、使用递归打印斐波那契数列(前两个数的和得到第三个数,如:0 1 1 2 3 4 7...)
# def func(a, b, stop):
#  if a > stop:
#     return
#  print(a, end=' ')
#  func(b, a + b, stop)
#
#
# func(0, 1, 10000)
# 12、一个嵌套很多层的列表,如l=[1,2,[3,[4,5,6,[7,8,[9,10,[11,12,13,[14,15]]]]]]],用递归取出所有的值
#
#  for x in l:
#     if type(x) is list:
#        func(x)
#     else:
#        print(x)
#
# func(l)
#
# 13、思考:判断下述说法是否正确
#     题目1:
#     1、应该将程序所有所有所有的功能都扔到一个模块中,然后通过导入模块的方式引用它们
# False
#     2、应该只将程序各部分组件共享的那一部分功能扔到一个模块中,然后通过导入模块的方式引用它们
# 
True
#     题目2:
#     运行python文件与导入python文件的区别是什么?
运行python文件是由解释器解释执行并识别语法
导入py文件是导入一个模块

#     运行的python文件产生的名称空间何时回收,为什么?
# 内置 全局存活
# 全局 程序结束时死亡
# 局部 临时存活
#     导入的python文件产生的名称空间何时回收,为什么?
模块关闭时
# 14、运行run.py,然后run.py中导入了模块m1、m2,请画出内置名称空间、各个全局名称空间的嵌套关系图
posted @ 2020-07-23 23:32  刘海子  阅读(8)  评论(0编辑  收藏  举报