zoukankan      html  css  js  c++  java
  • 3.26作业

    作业:
    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}的形式
    #def add_data():
    #user_list = []
    #with open('db.txt', 'r', encoding='utf-8') as f:
    # 方式一
    # for line in f:
    # name, sex, age, salary = line.strip().split(' ')
    # user_list.append(
    # {'name': name, '': sex, 'age': age, 'salary': salary}
    # )
    2 根据1得到的列表,取出薪资最高的人的信息
    # print(max(info, key=lambda dic:dic.get('salary')))

    3 根据1得到的列表,取出最年轻的人的信息
    # print(min(info, key=lambda dic:dic.get('age')))

    4、将names=['egon','alex_sb','wupeiqi','yuanhao']中的名字全部变大写
    # names=['egon','alex_sb','wupeiqi','yuanhao']
    # print([name.upper() for name in names])
    # print(list(map(lambda x:x.upper(), names)))

    5、将names=['egon','alex_sb','wupeiqi','yuanhao']中以sb结尾的名字过滤掉,然后保存剩下的名字长度
    # print(list(filter(lambda name:not name.endswith('sb'), names)))
    # print([name for name in names if not name.endswith('sb')])

    6、求文件a.txt中最长的行的长度(长度按字符个数算,需要使用max函数)
    # with open('a.txt', 'rt', encoding='utf-8') as f:
    # print(len(max(f, key=lambda x:len(x))))
    # tmp = [len(line) for line in f]
    # print(max(tmp))
    7、求文件a.txt中总共包含的字符个数?思考为何在第一次之后的n次sum求和得到的结果为0?(需要使用sum函数)
    # 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.txt','rt', encoding='utf-8') as f:
    info=[line.split(',') for line in f]
    cost=sum(int(price)*int(count) for _, price, count in info)
    print(cost)

    # 打印出所有商品的信息,格式为[{'name':'xxx','price':333,'count':3},...]
    with open('shopping.txt','rt', encoding='utf-8') as f:
    info=[{
    'name': line[0],
    'price': int(line[1]),
    'count': int(line[2]),
    } for line in [i.split(',') for i in f]]
    print(info)

    # 求单价大于10000的商品信息,格式同上
    with open('shopping.txt','rt', encoding='utf-8') as f:
    info=[{
    'name': j[0],
    'price': int(j[1]),
    'count': int(j[2]),
    } for j in [i.split(',') for i in f] if int(j[1]) > 10000]
    print(info)

    10、思考:判断下述说法是否正确
    题目1:
    1、应该将程序所有功能都扔到一个模块中,然后通过导入模块的方式引用它们

    2、应该只将程序各部分组件共享的那一部分功能扔到一个模块中,然后通过导入模块的方式引用它们
    正确
    题目2:
    运行python文件与导入python文件的区别是什么?
    一种被当程序执行,另一种被当模块导入,
    运行的python文件产生的名称空间何时回收,为什么?
    程序执行完成
    导入的python文件产生的名称空间何时回收,为什么?
    程序执行完成
  • 相关阅读:
    小波变换的引入,通俗易懂
    Leetcode 437. Path Sum III
    Leetcode 113. Path Sum II
    Leetcode 112 Path Sum
    Leetcode 520 Detect Capital
    Leetcode 443 String Compression
    Leetcode 38 Count and Say
    python中的生成器(generator)总结
    python的random模块及加权随机算法的python实现
    leetcode 24. Swap Nodes in Pairs(链表)
  • 原文地址:https://www.cnblogs.com/lijunc/p/12577263.html
Copyright © 2011-2022 走看看