“Ekspresi Generator Python” Kode Jawaban

Ekspresi Generator Python

# Initialize the list
my_list = [1, 3, 6, 10]

# square each term using list comprehension
list_ = [x**2 for x in my_list]

# same thing can be done using a generator expression
# generator expressions are surrounded by parenthesis ()
generator = (x**2 for x in my_list)

print(list_)
print(generator)
SAMER SAEID

Ekspresi Generator Python

>>> iterator = ('Hello' for i in range(3))
>>> for x in iterator:
...     print(x)
'Hello'
'Hello'
'Hello'
Magnificent Mole

Ekspresi Generator Python

>>> for x in even_squares:
...     print(x)
0
4
16
36
64
Magnificent Mole

Ekspresi Generator Python

def generator():
    for item in collection:
        yield expression
Magnificent Mole

Ekspresi Generator Python

>>> genexpr = ('Hello' for i in range(3))
>>> list(genexpr)
['Hello', 'Hello', 'Hello']
Magnificent Mole

Jawaban yang mirip dengan “Ekspresi Generator Python”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya