python的一些语法糖
l
=[i
for i in
range(10)]
print(l
)
#相当于
l
=[]
for i in
range(10):
l
.append(i
)
print(l
)
#偶数输出
l
=[i
for i in
range(10) if i
%2==0]
print(l
)
#相当于
l
=[]
for i in
range(10):
if i
%2==0:
l
.append(i
)
print(l
)
#生成字典的语法糖
d
={i
:i
+10 for i in
range(10)}
print(d
)
#相当于
d
={}
for i in
range(10):
d
[i
]=i
+10
print(d
)
d
={i
:"老蒋的女朋友" for i in
range(10)}
print(d
)
转载请注明原文地址:https://tech.qufami.com/read-18867.html