074❤列表推导式

一、列表推导式的使用

[expression for item1 in iterable1 if condition1
for item2 in iterable2 if condition2
...
for itemN in iterableN if conditionN
]
类似于
res=[]
for item1 in iterable1:
    if condition1:
        for item2 in iterable2:
            if condition2
                ...
                for itemN in iterableN:
                    if conditionN:
                        res.append(expression)
print(F" {[i for i in range(10)]}")
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(F"{[i**2 for i in range(10)]}")
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
posted @ 2020-09-11 23:07  AShine^_^  阅读(112)  评论(0编辑  收藏  举报