In [1]:
a = "abc"
In [2]:
dir(a)
Out[2]:
In [3]:
dir([])
Out[3]:
In [12]:
it = [1,2,3].__iter__()
In [13]:
while(it):
print(it.__next__())
In [14]:
dir([].__iter__())
Out[14]:
In [15]:
type([].__iter__())
Out[15]:
In [16]:
help(isinstance)
In [19]:
class Counter:
def __init__(self, stop):
self.current = 0
self.stop = stop
def __iter__(self):
return self
def __next__(self):
if self.current < self.stop:
r = self.current
self.current += 1
return r
else:
raise StopIteration
In [23]:
for i in Counter(4):
print(i, end='')
In [24]:
a,b,c = Counter(3)
In [25]:
print(a,b,c)
In [31]:
a,b,c = map(int, input().split())
In [9]:
class Count:
def __init__(self, stop):
self.stop = stop
def __getitem__(self, index):
if index < self.stop:
return index+10
else:
raise IndexError
In [10]:
c = Count(3)
In [11]:
print(c[0], c[1], c[2])
In [5]:
for i in Count(3):
print(i, end=' ')
In [12]:
it = iter(range(3))
In [13]:
next(it)
Out[13]:
In [14]:
next(it)
Out[14]:
In [15]:
next(it)
Out[15]:
In [16]:
next(it)
In [17]:
import random
In [21]:
it = iter(lambda: random.randint(0,5),2)
In [28]:
next(it)
In [29]:
next(it)
In [31]:
it = iter(range(3),1)
In [37]:
for i in iter(lambda: random.randint(0,5),2):
print(i, end=' ')
In [38]:
for i in range(10):
print(i, end='')
In [47]:
it = iter(range(2))
In [48]:
for i in range(20):
print(next(it,10))
In [49]:
def number_gener():
yield 0
yield 1
yield 2
In [50]:
for i in number_gener():
print(i, end=' ')
In [51]:
it = number_gener()
In [53]:
it.__iter__().__next__()
Out[53]:
In [54]:
it = iter(number_gener())
In [57]:
next(it)
Out[57]:
In [58]:
def one_generator():
yield 1
return 'return에 지정한 값'
try:
g = one_generator()
next(g)
next(g)
except StopIteration as e:
print(e) # return에 지정한 값
In [73]:
def num_gene(stop):
n = 0
while n < stop:
yield n
n += 1
In [60]:
for i in num_gene(3):
print(i)
In [62]:
def num_gene2(stop):
for n in range(stop): #여기서 range를 쓸거면 구지 제너레이션 기법을 안해도 될거 같다.
yield n
In [63]:
for i in num_gene2(4):
print(i)
In [64]:
def upper_gene(x):
for i in x:
yield i.upper()
In [66]:
a = ['aaa','bbb']
for i in upper_gene(a):
print(i)
In [67]:
def number_gene3():
x = [1,2,3]
yield from x
In [68]:
for i in number_gene3():
print(i)
In [76]:
def three_gene():
yield from num_gene(4)
In [77]:
for i in three_gene():
print(i)
In [78]:
[ i for i in range(50) if i % 2 == 0]
Out[78]:
In [80]:
for i in ( i for i in range(50) if i % 2 == 0):
print(i, end=' ')
In [89]:
for i in (i for i in range(50) if i % 2 == 0):
print(i, end=' ')
In [87]:
for i in [i for i in range(50) if i % 2 == 0]:
print(i, end=' ')
In [ ]: