레이블이 iterations인 게시물을 표시합니다. 모든 게시물 표시
레이블이 iterations인 게시물을 표시합니다. 모든 게시물 표시

6/07/2020

Python - Coding Problem: Creating Your Own Iterators

** Coding Problem: Creating Your Own Iterators


==========================================

my_sentence = Sentence('This is a test')

for word in my_sentence:
print(word)


# This should have the following output:
# This
# is
# a
# test

---------------------------------

class Sentence:

def __init__(self, sentence):
self.sentence = sentence
self.index = 0
self.words = self.sentence.split()

def __iter__(self):
return self

def __next__(self):
if self.index >= len(self.words):
raise StopIteration
index = self.index
self.index += 1
return self.words[index]


my_sentence = Sentence('This is a test')

for word in my_sentence:
print(word)

---------------------------------

This
is
a
test

==========================================
==========================================

class Sentence:

def __init__(self, sentence):
self.sentence = sentence
self.index = 0
self.words = self.sentence.split()

def __iter__(self):
return self

def __next__(self):
if self.index >= len(self.words):
raise StopIteration
index = self.index
self.index += 1
return self.words[index]


my_sentence = Sentence('This is a test')

print(next(my_sentence))
print(next(my_sentence))
print(next(my_sentence))
print(next(my_sentence))
print(next(my_sentence))

---------------------------------

is
a
test
    print(next(my_sentence))
  File "C:\Users\purunet\Documents\py9\ii.py", line 14, in __next__
    raise StopIteration
StopIteration

---------------------------------

class Sentence:

def __init__(self, sentence):
self.sentence = sentence
self.index = 0
self.words = self.sentence.split()

def __iter__(self):
return self

def __next__(self):
if self.index >= len(self.words):
raise StopIteration
index = self.index
self.index += 1
return self.words[index]

def sentence(sentence):
for word in sentence.split():
yield word


my_sentence = sentence('This is a test')


for word in my_sentence:
print(word)

---------------------------------

This
is
a
test

==========================================

5/05/2020

Python - Loops and Iterations

** Loops and Iterations


==========================================

nums = [1, 2, 3, 4, 5]

for num in nums:
print(num)

---------------------------------

1
2
3
4
5

---------------------------------

==========================================

nums = [1, 2, 3, 4, 5]

for num in nums:
if num == 3:
print('Found!!')
break
print(num)


---------------------------------

1
2
Found!!

---------------------------------

==========================================

nums = [1, 2, 3, 4, 5]

for num in nums:
if num == 3:
print('Found!!')
continue
print(num)


---------------------------------

1
2
Found!!
4
5

---------------------------------

==========================================

nums = [1, 2, 3, 4, 5]

for num in nums:
for letter in 'abc':
print(num, letter)

---------------------------------

1 a
1 b
1 c
2 a
2 b
2 c
3 a
3 b
3 c
4 a
4 b
4 c
5 a
5 b
5 c

---------------------------------

==========================================

for i in range(10):
print(i)

---------------------------------

0
1
2
3
4
5
6
7
8
9

---------------------------------

==========================================

for i in range(1, 11):
print(i)

---------------------------------

1
2
3
4
5
6
7
8
9
10

---------------------------------

==========================================

x = 0

while x < 10 :
print(x)
x += 1

---------------------------------

0
1
2
3
4
5
6
7
8
9

---------------------------------

==========================================

x = 0

while x < 10 :
if x == 5:
break
print(x)
x += 1

---------------------------------

0
1
2
3
4

---------------------------------

==========================================

x = 0

while True :
if x == 5:
break
print(x)
x += 1

---------------------------------

0
1
2
3
4

---------------------------------