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}的形式

    with open('score.txt', 'r', encoding='utf-8')as file:
        l1 = []
        for line in file:
            name, sex, age, salary = line.strip().split(' ')
            dic1 = {'name':name, 'sex':sex, 'age':int(age), 'salary':int(salary)}
            l1.append(dic1)
    print(l1)
    with open('score.txt')as f1:
        info=[{
            'name':line.strip().split(' ')[0],
            'sex':line.strip().split(' ')[1],
            'age':line.strip().split(' ')[2],
            'salary':line.strip().split(' ')[3]
       }for line in f1]

    2 根据1得到的列表,取出薪资最高的人的信息

    res=max(l1,key=lambda k:k.get('salary'))
    print(res)

    3 根据1得到的列表,取出最年轻的人的信息

    res1=min(l1,key=lambda k:k.get('age'))
    print(res1)

    4、将names=['egon','alex_sb','wupeiqi','yuanhao']中的名字全部变大写

    names=['egon','alex_sb','wupeiqi','yuanhao']
    new_names=[i.upper()  for i in names]
    print(new_names)

    5、将names=['egon','alex_sb','wupeiqi','yuanhao']中以sb结尾的名字过滤掉,然后保存剩下的名字长度

    names=['egon','alex_sb','wupeiqi','yuanhao']
    new_names=[i for i in names if not i.endswith('_sb')]
    print(new_names)

    6、求文件a.txt中最长的行的长度(长度按字符个数算,需要使用max函数)

    with open('a.txt','r',encoding='utf-8')as file:
        len_list=[]
        for line in file:
            len_list.append(len(line))
    print(max(len_list))
    with open('a.txt','r',encoding='utf-8')as file:
        len_list=[len(line) for line in file]
        print(max(len_list))

    7、求文件a.txt中总共包含的字符个数?思考为何在第一次之后的n次sum求和得到的结果为0?(需要使用sum函数)

    with open('a.txt','r',encoding='utf-8')as file:
        len_list=[len(line) for line in file]
        print(sum(len_list))

    8、思考题

    with open('a.txt') as f:
        g=(len(line) for line in f)
    print(g) #为何报错?

    # 需要在文件关闭前执行该操作

    9、文件shopping.txt内容如下

    mac,20000,3 lenovo,3000,10 tesla,1000000,10 chicken,200,1 求总共花了多少钱?

    with open('shopping.txt')as f1:
        price=[int(line.strip().split(',')[1])for line in f1]
        print(sum(price))

    打印出所有商品的信息,格式为[{'name':'xxx','price':333,'count':3},...]

    with open('shopping.txt')as f:
        info=[{'name':line.strip().split(',')[0],
               'price':line.strip().split(',')[1],
               'count':line.strip().split(',')[2]}for line in f]

        print(info)

    求单价大于10000的商品信息,格式同上

    with open('shopping.txt')as f2: info=[{ 'name':line.strip().split(',')[0], 'price':line.strip().split(',')[1], 'count':line.strip().split(',')[2] }for line in f2 if int(line.strip().split(',')[1])>10000] print(info)

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

        题目2:
       运行python文件与导入python文件的区别是什么?
        一个是编辑 一个 是运行代码
       运行的python文件产生的名称空间何时回收,为什么?
           运行完毕回收
       导入的python文件产生的名称空间何时回收,为什么?
            调用结束回收
  • 相关阅读:
    redis在java项目中的使用
    Nginx+Tomcat搭建高性能负载均衡集群
    Redis 数据类型
    MySQL 索引概述
    Spring boot 中的WebMvcConfigurerAdapter、WebMvcConfigurationSupport与WebMvcConfigurer区别
    DAO与DTO名词解释
    FindBugs-IDEA插件的使用
    Map 中有 HashMap、TreeMap、HashTable、LinkedHashMap,首先简单说一下他们之间的区别:
    javax.el.PropertyNotFoundException:
    内省(introspector)------>JavaBean
  • 原文地址:https://www.cnblogs.com/bailongcaptain/p/12577133.html
Copyright © 2011-2022 走看看