1. 从文件中取出每一条记录放入列表中,列表的每个元素都是{'name':'egon','sex':'male','age':18,'salary':3000}的形式
with open(r'db.txt',mode='rt', encoding='utf-8') as f:
l=[{'name':line.strip().split()[0],
'sex':line.strip().split()[1],
'age':line.strip().split()[2],
'salary':line.strip().split()[3]} for line in f]
print(l)
print(l)
2. 根据1得到的列表,取出薪资最高的人的信息
res = max(l, key=lambda k: k['salary'])
print(res)
3. 根据1得到的列表,取出最年轻的人的信息
younger = min(l,key=lambda dic: dic['age'])
print(younger)
4. 将names=['egon','alex_sb','wupeiqi','yuanhao']中的名字全部变大写
names=['egon','alex_sb','wupeiqi','yuanhao']
names=[name.upper() for name in names]
print(names)
names=['egon','alex_sb','wupeiqi','yuanhao']
# up = [name.upper() lambda name:name.upper()]
up = map(lambda name:name.upper(),names)
print(list(up))
5. 将names=['egon','alex_sb','wupeiqi','yuanhao']中以sb结尾的名字过滤掉,然后保存剩下的名字长度
names=['egon','alex_sb','wupeiqi','yuanhao']
names = [name for name in names if not name.endswith('sb')]
print(names)
res = filter(lambda l:not l.endswith('sb'),names)
print(list(res))
6. 求文件a.txt中最长的行的长度(长度按字符个数算,需要使用max函数)
with open(r'a.txt', mode='rt', encoding='utf-8') as f:
res = max(f, key=lambda line: len(line.strip()))
print(len(res.strip()))
with open(r'a.txt', mode='rt', encoding='UTF-8') as f:
text_len = max([len(line.strip()) for line in f])
print(text_len)
7. 求文件a.txt中总共包含的字符个数?思考为何在第一次之后的n次sum求和得到的结果为0?(需要使用sum函数)
with open(r'a.txt', mode='rt',encoding='utf-8') as f:
res = sum(len(line.strip()) for line in f)
res1 = sum(len(line.strip()) for line in f)
print(res)
print(res1)
'''
因为第一次sum求和时即第一次打开文件文件指针在文件开头,
当第一次sum求和完毕后文件指针在文件的末尾
此时当我们再次使用sum求值时由于文件没有关闭,
因此指针一直在文件的末尾因此当第一次之后的n次sum求和结果都为0
'''
8. 思考题
with open('a.txt') as f:
g = (len(line) for line in f)
print(sum(g)) # 为何报错?
#因为当我们在此处打印sum(g)求和时,文件f已经被关闭了,因此会报错
9. 文件shopping.txt内容如下
# mac,20000,3
# lenovo,3000,10
# tesla,1000000,10
# chicken,200,1
# 求总共花了多少钱?
with open(r'shopping.txt', mode='rt', encoding='utf-8') as f:
res = sum(int(line.strip().split(',')[1]) * int(line.strip().split(',')[2]) for line in f)
print(res)
with open(r'shopping.txt', mode='rt', encoding='utf-8') as f11:
res11 = sum(int(list[1]) * int(list[2]) for list in (line.strip().split(',') for line in f11))
print(res11)
# 打印出所有商品的信息,格式为[{'name':'xxx','price':333,'count':3},...]
with open(r'shopping.txt', mode='rt', encoding='utf-8') as f1:
res1 = [{'name': line.strip().split(',')[0],
'price': line.strip().split(',')[1],
'count': line.strip().split(',')[2]} for line in f1]
print(res1)
with open(r'shopping.txt', mode='rt', encoding='utf-8') as f2:
res2 = [{'name': dic[0],
'price': int(dic[1]),
'count': int(dic[2])} for dic in (line.strip().split(',') for line in f2)]
print(res2)
# 求单价大于10000的商品信息,格式同上
with open(r'shopping.txt', mode='rt', encoding='utf-8') as f2:
res2 = [{'name': dic[0],
'price': int(dic[1]),
'count': int(dic[2])} for dic in (line.strip().split(',') for line in f2)]
res3 = filter(lambda dic: dic['price'] > 10000, res2)
print(list(res3))
10. 思考:判断下述说法是否正确
# 题目1:
# 1、应该将程序所有功能都扔到一个模块中,然后通过导入模块的方式引用它们 (错)
# 2、应该只将程序各部分组件共享的那一部分功能扔到一个模块中,然后通过导入模块的方式引用它们 (对)
# 题目2:
# 运行python文件与导入python文件的区别是什么?
'''
运行python文件指运行python程序
导入python文件指将python文件作为模块,在需要的地方引用模块python文件得功能代码
'''
# 运行的python文件产生的名称空间何时回收,为什么?
'''
python文件运行结束后回收,python文件得主程序全部运行完毕后结束对名称空间内名字得使用,名称空间就会被回收
'''
# 导入的python文件产生的名称空间何时回收,为什么?
'''
python文件运行结束后回收,因为导入得python文件是模块,只有当主程序运行结束后,被引用得python文件模块才会被回收
'''
11. 二分法
list1 = [1, 3, 5, 7, 9, 11, 13, 15, 17, 18, 22]
def func(find_number, l):
print(l)
if len(l) == 0:
print('error')
return
num = len(l) // 2
if l[num] > find_number:
l = l[:num]
func(find_number, l)
elif l[num] < find_number:
l = l[num + 1:]
func(find_number, l)
else:
print('find it')
func(2, list1)