[Python's] Python's list comprehensions a

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Python's] Python's list comprehensions a相关的知识,希望对你有一定的参考价值。

# Python‘s list comprehensions are awesome.

vals = [expression 
        for value in collection 
        if condition]

# This is equivalent to:

vals = []
for value in collection:
    if condition:
        vals.append(expression)

# Example:

>>> even_squares = [x * x for x in range(10) if not x % 2]
>>> even_squares
[0, 4, 16, 36, 64]

 

以上是关于[Python's] Python's list comprehensions a的主要内容,如果未能解决你的问题,请参考以下文章

Python中map()问题

python 类实例化

python如何去除字符串里面的空格

python TypeError: unsupported operand type(s) for +: 'geoprocessing value object' and 's

python 当i!= 1时,多个单词(添加's')

python中的set有啥用