python列表、集合、元祖、字典推导式

a = [1, 2, 3, 4, 5, 6, 7, 8]
l=[i**2 for i in a if i**2>=16] #列表推导式+if判断
print(l)
print(type(l))

b={1:1,2:2,3:3}
d={i**2 for i in b }
print(d) #输出的是一个集合
print(type(d))

c=(1,2,3,4,5) #元组,输出的是生成器
t=(i**2 for i in c)
print(t)
print(type(t))

d1={1,2,3,4,5,6} #集合
s={i**2 for i in d1}
print(s)
print(type(s))


a1=[1,2,3]
b1={i:i*2 for i in a1 } #b1是字典格式

print(b1)
print(type(b1))

 

猜你喜欢

转载自www.cnblogs.com/lanyy/p/10706677.html