“Iterator Python” Kode Jawaban

Python iterasi melalui iterator

# define a list
my_list = [4, 7, 0, 3]
# get an iterator using iter()
my_iter = iter(my_list)
# iterate through it using next()
# Output: 4
print(next(my_iter))
# Output: 7
print(next(my_iter))
# next(obj) is same as obj.__next__()

# Output: 0
print(my_iter.__next__())

# Output: 3
print(my_iter.__next__())

# This will raise error, no items left
next(my_iter)
SAMER SAEID

Iterator Python


def iter(times):
    it = 1
    for i in range(times):
        its = it * 2
        it = its
    return it

while True:
    print()
    print(iter(int(input("how many iteration? : "))))
khuDDD

Iterator Python

fruits = ["apples", "bananas", "cherries", "pears", "plums"]
fruitIterator = iter(fruits);
print(next(fruitIterator))
print(next(fruitIterator))
print(next(fruitIterator))
Javasper

Jawaban yang mirip dengan “Iterator Python”

Pertanyaan yang mirip dengan “Iterator Python”

Lebih banyak jawaban terkait untuk “Iterator Python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya