标签:for def 变量 bsp app lam else for循环 迭代
目的:方便的生成一个列表
格式:
v1 = [i for i in 可迭代对象 ]
v2 = [i for i in 可迭代对象 if条件]#条件为True菜进行append
v1 = [i for i in ‘alex‘]
# 变量 = [for循环的变量 for循环一个可迭代的对象]
v2 = [i+100 for i in range(10)]
v3 = [100 for i in range(10)]
v4 = [99 if i > 5 else 66 for i in range(10)]
def func():
print(123)
v5 = [func for i in range(10)]
v6 = [lambda :i for i in range(10)]
v7 = [lambda x:x*i for i in range(10)]
print(v1)
print(v2)
print(v3)
print(v4)
print(v5)
print(v6[9]())
print(v7[0](2))
# 筛选功能
v8 = [i for i in range(10) if i >5]
print(v8)
格式:v1 = {i for i in 可迭代对象}
v8 = {i for i in range(10) if i >5} print(v8)
格式:v1 = {‘k‘+str(i):i for i in range(10)}
v1 = {‘k‘+str(i):i for i in range(10)} print(v1)
标签:for def 变量 bsp app lam else for循环 迭代
原文地址:https://www.cnblogs.com/wangdianchao/p/11519675.html