python正常遍历写的长度都太长,但是python有个推导式可以减少这个长度,比如这样,你想要遍历这个列表,并且复制里面长度大于3的字符串到name_list这个列表里并且打印一下,正常写就得一个循环。如下:
names: list[str] = ['Sara', 'John', 'Mike', 'Catalina', 'Bob', 'Tom', 'Alice', 'Jerry', 'Wendy', 'Smith']
name_list: list[str] = []
for name in names:
if len(name) > 3:
name_list.append(name)
print(name_list)
但是如果你使用了推导式就会变的非常的短且也不难理解。
names: list[str] = ['Sara', 'John', 'Mike', 'Catalina', 'Bob', 'Tom', 'Alice', 'Jerry', 'Wendy', 'Smith']
name_list: list[str] = [name for name in names if len(name) > 3]
print(name_list)
把作为临时变量的name提到for的前面去作为一个表达式,然后在末尾可以插入if或者再插入一层for进行循环都是可以的。然后根据自己的需求就能完成推导式了。但本身返回的对象是<generator object <genexpr> at 0x0000018CE8B86260>,是一个生成器,所以需要使用对应的[],(),{}来包裹住,当然想要变成字典的话也可以使用冒号来。
接下来让我们看一下很抽象的推导式
l: list[int] = [i ** 2 for i in range(1, 11) if i % 3 != 0 if i != 1 for i in range(1, 11)]
print(l)
可能你会看不懂,他的答案是
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
那假如给他化成普通形式,看看是不是就能看懂了
l: list[int] = [i ** 2 for i in range(1, 11) if i % 3 != 0 if i != 1 for i in range(1, 11)]
l2: list[int] = []
for i in range(1, 11):
if i % 3 != 0:
if i != 1:
for i in range(1, 11):
l2.append(i ** 2)
print(l2)
print(l)
print(l == l2)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
True
相等的结果是不是为True,所以推导式就是通过for与if把层层递进的结果算出来返回给最外面的值然后生成一个生成器,我们这里使用[]去接收,类似我们下面正常的循环里面使用l2去append一样。
然后再看看这个
l: list[int] = [i ** 2 for i in range(1, 11) if i % 3 != 0 if i != 1 for i in range(1, 11)]
l2: list[int] = []
for i in range(1, 11):
if i % 3 != 0:
if i != 1:
for j in range(1, 11):
l2.append(i ** 2)
print(l)
print(l2)
print(l == l2)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100]
False
想一想如果把推导式末尾的i换成j是不是就和下面的l2一致了,我们继续试一试
l: list[int] = [i ** 2 for i in range(1, 11) if i % 3 != 0 if i != 1 for j in range(1, 11)]
l2: list[int] = []
for i in range(1, 11):
if i % 3 != 0:
if i != 1:
for j in range(1, 11):
l2.append(i ** 2)
print(l)
print(l2)
print(l == l2)
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100]
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100]
True
现在应该看明白推导式了吧,假如你用其他的方式去替代也都是一样的,只不过将生成器和我们的这个l2换了个类型,比如我们使用集合来,毕竟打印出来的相同字符还是太多了。我们直接用集合来去重
l: set[int] = {i ** 2 for i in range(1, 11) if i % 3 != 0 if i != 1 for j in range(1, 11)}
l2: set[int] = set()
for i in range(1, 11):
if i % 3 != 0:
if i != 1:
for j in range(1, 11):
l2.add(i ** 2)
print(l)
print(l2)
print(l == l2)
{64, 4, 100, 16, 49, 25}
{64, 4, 100, 16, 49, 25}
True
完全一致,不相信的大家可以拷贝代码来运行。
相信大家已经完全明白了基础的推导式了,接下来就来看字典推导式,实际上和普通的推导式是一样的,但是呢我们多一个映射
l: dict[int:int] = {i: i ** 2 for i in range(1, 11) if i % 3 != 0 if i != 1 for j in range(1, 11)}
l2: dict[int:int] = {}
for i in range(1, 11):
if i % 3 != 0:
if i != 1:
for j in range(1, 11):
l2[i] = i ** 2
print(l)
print(l2)
print(l == l2)
{2: 4, 4: 16, 5: 25, 7: 49, 8: 64, 10: 100}
{2: 4, 4: 16, 5: 25, 7: 49, 8: 64, 10: 100}
True
相信基本的使用大家都已经会了,只需要记住推导式只有for和if,然后呢可以一直嵌套,只要能搞明白正常要写下去是怎么样的,化成推导式也就简单了,自然,如果熟练了之后呢,也可以直接写推导式
如果对你有帮助的话不要忘记点个赞或者点个收藏