大鹏

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
#题目一
names=['egon','alex_sb','wupeiqi','yuanhao']
names=[name.upper() for name in names]

#题目二
names=['egon','alex_sb','wupeiqi','yuanhao']
names=[len(name) for name in names if not name.endswith('sb')]

#题目三
with open('a.txt',encoding='utf-8') as f:
    print(max(len(line) for line in f))

#题目四
with open('a.txt', encoding='utf-8') as f:
    print(sum(len(line) for line in f))
    print(sum(len(line) for line in f)) #求包换换行符在内的文件所有的字符数,为何得到的值为0?
    print(sum(len(line) for line in f)) #求包换换行符在内的文件所有的字符数,为何得到的值为0?

#题目五(略)

#题目六:每次必须重新打开文件或seek到文件开头,因为迭代完一次就结束了
with open('a.txt',encoding='utf-8') as f:
    info=[line.split() for line in f]
    cost=sum(float(unit_price)*int(count) for _,unit_price,count in info)
    print(cost)


with open('a.txt',encoding='utf-8') as f:
    info=[{
        'name': line.split()[0],
        'price': float(line.split()[1]),
        'count': int(line.split()[2]),
    } for line in f]
    print(info)


with open('a.txt',encoding='utf-8') as f:
    info=[{
        'name': line.split()[0],
        'price': float(line.split()[1]),
        'count': int(line.split()[2]),
    } for line in f if float(line.split()[1]) > 10000]
    print(info)

 

posted on 2018-12-16 22:51  pf42280  阅读(99)  评论(0)    收藏  举报