#列表生成器
l=["xiaozhan","wangyibo_dsb","zhuyilong_dsb","huyitian"]
# l_new=[]
# for name in l:
# if name.endswith("dsb"):
# l_new.append(name)
# print(l_new)
#类似于三元表达式,符合条件的放在左边
# l_new=[name for name in l if name.endswith("dsb")]
# l_new1=[name for name in l]
# print(l_new)
# print(l_new1)
#把所有小写字母变成大写的
# l_big=[name.upper() for name in l ]
# print(l_big)
#
# #把所有名字的去掉后缀_dsb
# l_new=[name.replace("_dsb","")for name in l]
# print(l_new)
"""字典生成器"""
# keys=['name','age','gender']
# dic={key:None for key in keys}
# print(dic)
# keys=[('name',"egon"),('age',18),('gender','male')]
# res={k:v for k, v in keys if k!="gender"}
# print(res)
"""集合生成器"""
# keys=['name','age','gender']
# res={key for key in keys}
# print(res)
##没有元组生成器
"""生成器表达式"""
# g=(i for i in range(10) if i > 3) #---> 此时g内部一个值都没有
#计算一个文件中有多少字符
"""方式一"""
# with open("a.txt",mode='rt',encoding="utf-8") as f:
# res=0
# for line in f:
# res+=len(line)
# print(res)
"""方式二"""
# 行数太多了不好
# with open("a.txt",mode='rt',encoding="utf-8") as f:
# res=sum([len(line) for line in f])
# print(res)
"""方式三"""
#效率最高 --->用生成器表达式
# with open("a.txt",mode='rt',encoding="utf-8") as f:
# g=(len(line) for line in f)
# print(g)
# res=sum(g)
# print(res)
with open("a.txt",mode='rt',encoding="utf-8") as f:
res=sum(len(line) for line in f)
print(res)