Featured Post

Set up machine learning and deep learning on AWS

Here is the simple instructions to set up a EC2 instance to run machine learning and deep learning on AWS 1.  Run an EC2 instance from ...

Apr 24, 2020

Python - list comprehension examples

Python Logo Png | Transparent PNG Download #616238 - Vippng

List comprehension is a Python feature to process elements in the list without writing a function.

Square of numbers from 0 to n

Python script
for x in range(10):
    print(x**2)

List comprehension
[x**2 for x in range(n)]

Map function
map(lambda x: x**2, range(n))

Square of even numbers from 0 to n

Python script
for x in range(10):
    if x%2 == 0:
        print(x**2)

List comprehension with if clause
[x**2 for x in range(n) if x%2 == 0]


List comprehension with two items
[x + y for x in range(10) for y in range(10)]
[(x, y, x+y) for x in range(10) for y in range(10)]

List comprehension with two items with if clause
[(x, y, x+y) for x in range(10) for y in range(10) if (x%2==0) & * (y%2==0)]

1 comment:

  1. These video games usually require minimal skill and contain a component of chance. He would 코인카지노 finally blow by way of “a couple hundred thousand dollars” before admitting he had turn out to be an issue gambler in May. Still, Jason considers himself “a slightly better case than common.” Since he joined Gamblers Anonymous, he’s heard tales of individuals dropping properties and dwelling out of their automotive.

    ReplyDelete