Chapter folders renamed
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
Iteration is one of the key tools used for data processing and data transformation.
|
||||
The iterations are especially useful when dealing with large datasets and bringing
|
||||
the whole dataset into the memory is not possible or efficient. Iterators provide
|
||||
a way to bring the data into memory one item at a time.
|
||||
@@ -0,0 +1,12 @@
|
||||
#generators1.py
|
||||
def my_gen():
|
||||
yield 'A'
|
||||
yield 'B'
|
||||
yield 'C'
|
||||
|
||||
|
||||
if(__name__ == "__main__"):
|
||||
iter1 = my_gen()
|
||||
print(iter1.__next__())
|
||||
print(next(iter1))
|
||||
print(iter1.__next__())
|
||||
@@ -0,0 +1,19 @@
|
||||
#generator2.py
|
||||
class Week:
|
||||
def __init__(self):
|
||||
self.days = {1:'Monday', 2: "Tuesday",
|
||||
3:"Wednesday", 4: "Thursday",
|
||||
5:"Friday", 6:"Saturday", 7:"Sunday"}
|
||||
|
||||
def week_gen(self):
|
||||
for x in self.days:
|
||||
yield self.days[x]
|
||||
|
||||
if(__name__ == "__main__"):
|
||||
wk = Week()
|
||||
iter1 = wk.week_gen()
|
||||
iter2 = iter(wk.week_gen())
|
||||
print(iter1.__next__())
|
||||
print(iter2.__next__())
|
||||
print(next(iter1))
|
||||
print(next(iter2))
|
||||
@@ -0,0 +1,7 @@
|
||||
#generator3.py
|
||||
L = [1,2,3,4,5,6,7,8,9,0]
|
||||
f1 = [x+1 for x in L]
|
||||
g1 = (x+1 for x in L)
|
||||
|
||||
print(g1.__next__())
|
||||
print(g1.__next__())
|
||||
@@ -0,0 +1,14 @@
|
||||
#generator4.py
|
||||
def prime_gen(num):
|
||||
for cand in range(2, num+1):
|
||||
for i in range (2, cand):
|
||||
if (cand % i) == 0:
|
||||
break
|
||||
else:
|
||||
yield cand
|
||||
|
||||
def x2_gen(list2):
|
||||
for num in list2:
|
||||
yield num*num
|
||||
|
||||
print(sum(x2_gen(prime_gen(5))))
|
||||
@@ -0,0 +1,20 @@
|
||||
#iterator1.py
|
||||
#example 1: iterating on a list
|
||||
for x in [1,2,3]:
|
||||
print(x)
|
||||
|
||||
#example 2: iterating on a string
|
||||
for x in "Python for Geeks":
|
||||
print(x, end="")
|
||||
print('')
|
||||
|
||||
#example 3: iterating on a dictionary
|
||||
week_days = {1:'Mon', 2:'Tue',
|
||||
3:'Wed', 4:'Thu',
|
||||
5:'Fri', 6:'Sat', 7:'Sun'}
|
||||
for k in week_days:
|
||||
print(k, week_days[k])
|
||||
|
||||
#example 4: iterating on a file
|
||||
for row in open('abc.txt'):
|
||||
print(row, end="")
|
||||
@@ -0,0 +1,25 @@
|
||||
#iterator2.py
|
||||
class Week:
|
||||
def __init__(self):
|
||||
self.days = {1:'Monday', 2: "Tuesday",
|
||||
3:"Wednesday", 4: "Thursday",
|
||||
5:"Friday", 6:"Saturday", 7:"Sunday"}
|
||||
self._index = 1
|
||||
|
||||
def __iter__(self):
|
||||
self._index = 1
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
|
||||
if self._index < 1 | self._index > 8:
|
||||
raise StopIteration
|
||||
else:
|
||||
ret_value = self.days[self._index]
|
||||
self._index +=1
|
||||
return ret_value
|
||||
|
||||
if(__name__ == "__main__"):
|
||||
wk = Week()
|
||||
for day in wk:
|
||||
print(day)
|
||||
@@ -0,0 +1,29 @@
|
||||
#iterator3.py
|
||||
class Week:
|
||||
def __init__(self):
|
||||
self.days = {1:'Monday', 2: "Tuesday",
|
||||
3:"Wednesday", 4: "Thursday",
|
||||
5:"Friday", 6:"Saturday", 7:"Sunday"}
|
||||
self._index = 1
|
||||
|
||||
def __iter__(self):
|
||||
self._index = 1
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
|
||||
if self._index < 1 | self._index > 8:
|
||||
raise StopIteration
|
||||
else:
|
||||
ret_value = self.days[self._index]
|
||||
self._index +=1
|
||||
return ret_value
|
||||
|
||||
if(__name__ == "__main__"):
|
||||
wk = Week()
|
||||
iter1 = iter(wk)
|
||||
iter2 = iter(wk)
|
||||
print(iter1.__next__())
|
||||
print(iter2.__next__())
|
||||
print(next(iter1))
|
||||
print(next(iter2))
|
||||
@@ -0,0 +1,34 @@
|
||||
#iterator4.py
|
||||
class Week:
|
||||
def __init__(self):
|
||||
self.days = {1: 'Monday', 2: "Tuesday",
|
||||
3: "Wednesday", 4: "Thursday",
|
||||
5: "Friday", 6: "Saturday", 7: "Sunday"}
|
||||
|
||||
def __iter__(self):
|
||||
return WeekIterator(self.days)
|
||||
|
||||
class WeekIterator:
|
||||
def __init__(self, dayss):
|
||||
self.days_ref = dayss
|
||||
self._index = 1
|
||||
|
||||
def __iter__(self):
|
||||
return self;
|
||||
|
||||
def __next__(self):
|
||||
if self._index < 1 | self._index > 8:
|
||||
raise StopIteration
|
||||
else:
|
||||
ret_value = self.days_ref[self._index]
|
||||
self._index +=1
|
||||
return ret_value
|
||||
|
||||
if(__name__ == "__main__"):
|
||||
wk = Week()
|
||||
iter1 = iter(wk)
|
||||
iter2 = iter(wk)
|
||||
print(iter1.__next__())
|
||||
print(iter2.__next__())
|
||||
print(next(iter1))
|
||||
print(next(iter2))
|
||||
Reference in New Issue
Block a user