三元表达式
# 三元表达式print("sb") if 1==1 else print("shai")
列表生成式
# 列表生成字符串定制#egg_list=["egg:%s" %i for i in range(100) if i>50]print(egg_list)li=[1,2,3,4]l2="hello"list=[(i,j) for i in li if i>2 for j in l2]print(list)##import osg=os.walk('/Users/Alex/Desktop/photos')for i in g: print(i)print("-----------------------------")g = os.walk('/Users/Alex/Desktop/svg')glist=["%s/%s"%(i[0],j) for i in g for j in i[-1]]print(glist)#g=os.walk('/Users/Alex/Desktop')f_list=[]for i in g: for j in i[-1]: f_list.append("%s/%s"%(i[0],j))# 列表表达式egg_list=["egg:%s" %i for i in range(100)]
生成器表达式
# 生成器表达式f=("egg:%s" %i for i in range(100))print(next(f))f=open("a.txt","r")f1=(line.strip() for line in f)print(next(f1))