博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python-code-14
阅读量:5086 次
发布时间:2019-06-13

本文共 3985 字,大约阅读时间需要 13 分钟。

今日作业:    1、求文件a.txt中总共包含的字符个数?    2、思考题  思考为何在第一次之后的n次sum求和得到的结果为0?       with open('a.txt',encoding='utf-8') as f:          g=(len(line) for line in f)       print(sum(g))    3、文件shopping.txt内容如下       mac,2000,3       lenovo,3000,10       tesla,1000000,10       chicken,200,1       求总共花了多少钱?       打印出所有的商品信息,格式为       [{'name':'xxx','price':'3333','count':3},....]       求单价大于10000的商品信息,格式同上    4、文件内容如下,标题为:姓名,性别,年纪,薪资       egon male 18 3000       alex male 38 30000       wupeiqi female 28 20000       yuanhao female 28 10000       要求:       从文件中取出每一条记录放入列表中,       列表的每个元素都是{'name':'egon','sex':'male','age':18,'salary':3000}的形式    5 根据1得到的列表,取出薪资最高的人的信息    6 根据1得到的列表,取出最年轻的人的信息    7 根据1得到的列表,将每个人的信息中的名字映射成首字母大写的形式    8 根据1得到的列表,过滤掉名字以a开头的人的信息
##1sum_file = 0with open('a.txt',encoding='utf-8') as f:   for line in f:       sum_file += len(line)print(sum_file)with open('a.txt',encoding='utf-8') as f:    g=(len(line) for line in f)    print(sum(g))##2#g是生成器,生成器的取值特性就是从头到尾取完就结束,sum迭代取值第一次就取完了,所以后面g都是0,即sum也都是0##3total =0with open('shopping.txt',encoding='utf-8') as f:    for line in f:        line = line.strip('\n').split(',')        price = int(line[1])*int(line[2])        total += priceprint(total)with open('shopping.txt',encoding='utf-8') as f:    g = (int(line.strip('\n').split(',')[1])*int(line.strip('\n').split(',')[2]) for line in f)    print(sum(g))shopping_info = []with open('shopping.txt',encoding='utf-8') as f:    for line in f:        line = line.strip('\n').split(',')        keys = ['name','price','count']        d = {k:v for k,v in zip(keys,line)}        shopping_info.append(d)        print(d)print(shopping_info)shopping_info = []with open('shopping.txt',encoding='utf-8') as f:    for line in f:        line = line.strip('\n').split(',')        if int(line[1]) > 10000:            keys = ['name','price','count']            d = {k:v for k,v in zip(keys,line)}            shopping_info.append(d)print(shopping_info)##4user_info = []with open('4.txt',encoding='utf-8') as f:    for line in f:        line = line.split()        keys = ['name','sex','age','salary']        d = {k:v for k,v in zip(keys,line)}        user_info.append(d)print(user_info)##5user_info = []with open('4.txt',encoding='utf-8') as f:    for line in f:        line = line.split()        keys = ['name','sex','age','salary']        d = {k:v for k,v in zip(keys,line)}        user_info.append(d)print(user_info)salary_list = [int(index['salary']) for index in user_info]# for index in user_info:#     salary_list.append(int(index['salary']))res = max(salary_list)with open('4.txt',encoding='utf-8') as f:    for line in f:        if res == int(line.split()[3]):            print(line)##6user_info = []with open('4.txt',encoding='utf-8') as f:    for line in f:        line = line.split()        keys = ['name','sex','age','salary']        d = {k:v for k,v in zip(keys,line)}        user_info.append(d)print(user_info)age_info = (int(age['age']) for age in user_info)# for age in user_info:#     age = int(age['age'])#     age_info.append(age)for info in user_info:    if int(info['age']) == min(age_info):        print(info)##7user_info = []with open('4.txt', encoding='utf-8') as f:    for line in f:        line = line.split()        keys = ['name', 'sex', 'age', 'salary']        d = {k: v for k, v in zip(keys, line)}        user_info.append(d)print(user_info)for name in user_info:    name['name'] = name['name'].capitalize()print(user_info)##8user_info = []with open('4.txt', encoding='utf-8') as f:    for line in f:        line = line.split()        keys = ['name', 'sex', 'age', 'salary']        d = {k: v for k, v in zip(keys, line)}        user_info.append(d)print(user_info)new_user_info = [aname for aname in user_info if not aname['name'].startswith('a')]# new_user_info = []# for aname in user_info:#     if not aname['name'].startswith('a'):#         new_user_info.append(aname)print(new_user_info)
View Code

 

转载于:https://www.cnblogs.com/xujinjin18/p/9181275.html

你可能感兴趣的文章
一步步教你学会browserify
查看>>
Jmeter入门实例
查看>>
亲近用户—回归本质
查看>>
中文脏话识别的解决方案
查看>>
CSS之不常用但重要的样式总结
查看>>
Python编译错误总结
查看>>
URL编码与解码
查看>>
日常开发时遇到的一些坑(三)
查看>>
Eclipse 安装SVN插件
查看>>
深度学习
查看>>
TCP粘包问题及解决方案
查看>>
构建之法阅读笔记02
查看>>
添加按钮
查看>>
移动端页面开发适配 rem布局原理
查看>>
Ajax中文乱码问题解决方法(服务器端用servlet)
查看>>
会计电算化常考题目一
查看>>
阿里云服务器CentOS6.9安装Mysql
查看>>
剑指offer系列6:数值的整数次方
查看>>
js 过滤敏感词
查看>>
poj2752 Seek the Name, Seek the Fame
查看>>