“Python -Generatorausdruck” Code-Antworten

Python -Generatorausdruck

# 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

Generatorausdrücke Python

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

Generatorausdrücke Python

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

Generatorausdrücke Python

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

Generatorausdrücke Python

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

Ähnliche Antworten wie “Python -Generatorausdruck”

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen