“Generator Python” Kode Jawaban

Generator Python

def generador():
    n = 1
    yield n

    n += 1
    yield n

    n += 1
    yield n
Annoyed Armadillo

Generator Python

def count_to_ten_generator():
  for number in range(10):
    yield number
my_generator = count_to_ten_generator()
first_number = next(my_generator)
list_or_the_rest = list(my_generator)
Combative Cat

Fungsi Generator Python

def gen_func():
	for i in range(10):
    	yield i
Defeated Deer

Fungsi Generator Python

def gen_nums():
    n = 0
    while n < 4:
        yield n
        n += 1
Jealous Jay

Generator Python

# A recursive generator that generates Tree leaves in in-order.
def inorder(t):
    if t:
        for x in inorder(t.left):
            yield x

        yield t.label

        for x in inorder(t.right):
            yield x
Blushing Beaver

Generator Python

# Size of generators is a huge advantage compared to list
import sys

n= 80000

# List
a=[n**2 for n in range(n)]

# Generator
# Be aware of the syntax to create generators, lika a list comprehension but with round brakets
b=(n**2 for n in range(n))

print(f"List: {sys.getsizeof(a)} bits\nGenerator: {sys.getsizeof(b)} bits")
Frantic Frog

Jawaban yang mirip dengan “Generator Python”

Pertanyaan yang mirip dengan “Generator Python”

Lebih banyak jawaban terkait untuk “Generator Python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya