Home  >  Article  >  Backend Development  >  Python列表生成器的循环技巧分享

Python列表生成器的循环技巧分享

WBOY
WBOYOriginal
2016-06-06 11:21:511223browse

列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式。

一个循环

在C语言等其他语言中,for循环一般是这样的

如果python也这么写,那么真该看下python的基础教程了~

但要注意的是,需要加一个[]来,否则会报错...

在上面的例子中,不仅可以嵌套for,甚至可以嵌套if语句

再看看,原来是什么样子


两个循环呢?

原来可能是这样的?

现在可以这样了!!!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn