Skip to content

Commit 7146dc7

Browse files
committed
Add itertools lesson to the repo
1 parent 0a23dbf commit 7146dc7

9 files changed

Lines changed: 76 additions & 0 deletions

File tree

README.de.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ Es gibt zwei Möglichkeiten, die Module auszuführen:
101101
- Deque: [deque](ultimatepython/data_structures/deque.py) ( 🤯 )
102102
- Namedtuple: [namedtuple](ultimatepython/data_structures/namedtuple.py) ( 🤯 )
103103
- Defaultdict: [defaultdict](ultimatepython/data_structures/defaultdict.py) ( 🤯 )
104+
- Iterator-Tools: [Iterator-Tools](ultimatepython/data_structures/itertools.py) ( 🤯 )
104105
- Time complexity: [cPython operations](https://wiki.python.org/moin/TimeComplexity) ( 📚, 🤯 )
105106
4. **Klassen**
106107
- Basic class: [Basic definition](ultimatepython/classes/basic_class.py) ( 🍰 )

README.es.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ Hay dos maneras de ejecutar los módulos:
9999
- Deque: [deque](ultimatepython/data_structures/deque.py) ( 🤯 )
100100
- Namedtuple: [namedtuple](ultimatepython/data_structures/namedtuple.py) ( 🤯 )
101101
- Defaultdict: [defaultdict](ultimatepython/data_structures/defaultdict.py) ( 🤯 )
102+
- Herramientas de iteradores: [Herramientas de iteradores](ultimatepython/data_structures/itertools.py) ( 🤯 )
102103
- Complejidad de tiempo: [Operaciones de cPython](https://wiki.python.org/moin/TimeComplexity) ( 📚, 🤯 )
103104
4. **Clases**
104105
- Clase básica: [Definición de básica](ultimatepython/classes/basic_class.py) ( 🍰 )

README.fr.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Deux méthodes sont possibles :
109109
    - Deque : [deque](ultimatepython/data_structures/deque.py) ( 🤯 )
110110
    - Namedtuple : [namedtuple](ultimatepython/data_structures/namedtuple.py) ( 🤯 )
111111
    - Defaultdict : [defaultdict](ultimatepython/data_structures/defaultdict.py) ( 🤯 )
112+
    - Outils d'itérateurs : [Outils d'itérateurs](ultimatepython/data_structures/itertools.py) ( 🤯 )
112113
    - Complexité temporelle : [Opérations CPython](https://wiki.python.org/moin/TimeComplexity) ( 📚, 🤯 )
113114

114115
4. **Classes**

README.hi.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ print("Ultimate Python स्टडी गाइड")
8282
- डेक: [डेक](ultimatepython/data_structures/deque.py) ( 🤯 )
8383
- नामित ट्यूपल: [नामित ट्यूपल](ultimatepython/data_structures/namedtuple.py) ( 🤯 )
8484
- डिफ़ॉल्ट डिक्ट: [डिफ़ॉल्ट डिक्ट](ultimatepython/data_structures/defaultdict.py) ( 🤯 )
85+
- इटरेटर टूल्स: [इटरेटर टूल्स](ultimatepython/data_structures/itertools.py) ( 🤯 )
8586
- समय कोम्पलेक्सिटी: [cPython ऑपरेशन्स](https://wiki.python.org/moin/TimeComplexity) ( 📚, 🤯 )
8687
4. **क्लासेज़**
8788
- बेसिक क्लास: [बेसिक परिभाषा](ultimatepython/classes/basic_class.py) ( 🍰 )

README.ko.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ print("Ultimate Python 학습 가이드")
9090
- 덱: [deque](ultimatepython/data_structures/deque.py) ( 🤯 )
9191
- Namedtuple: [namedtuple](ultimatepython/data_structures/namedtuple.py) ( 🤯 )
9292
- Defaultdict: [defaultdict](ultimatepython/data_structures/defaultdict.py) ( 🤯 )
93+
- 이터레이터 도구: [이터레이터 도구](ultimatepython/data_structures/itertools.py) ( 🤯 )
9394
- 시간 복잡도 : [cPython 연산](https://wiki.python.org/moin/TimeComplexity) ( 📚, 🤯 )
9495
4. **클래스**
9596
- 기본 클래스 : [기본 정의](ultimatepython/classes/basic_class.py) ( 🍰 )

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ There are two ways of running the modules:
102102
- Deque: [deque](ultimatepython/data_structures/deque.py) ( 🤯 )
103103
- Namedtuple: [namedtuple](ultimatepython/data_structures/namedtuple.py) ( 🤯 )
104104
- Defaultdict: [defaultdict](ultimatepython/data_structures/defaultdict.py) ( 🤯 )
105+
- Itertools: [Iterator tools](ultimatepython/data_structures/itertools.py) ( 🤯 )
105106
- Time complexity: [cPython operations](https://wiki.python.org/moin/TimeComplexity) ( 📚, 🤯 )
106107
4. **Classes**
107108
- Basic class: [Basic definition](ultimatepython/classes/basic_class.py) ( 🍰 )

README.pt_br.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Existem duas maneiras de rodar os módulos:
8888
- Deque: [deque](ultimatepython/data_structures/deque.py) ( 🤯 )
8989
- Namedtuple: [namedtuple](ultimatepython/data_structures/namedtuple.py) ( 🤯 )
9090
- Defaultdict: [defaultdict](ultimatepython/data_structures/defaultdict.py) ( 🤯 )
91+
- Ferramentas de iteradores: [Ferramentas de iteradores](ultimatepython/data_structures/itertools.py) ( 🤯 )
9192
- Time complexity: [Operações de cPython](https://wiki.python.org/moin/TimeComplexity) ( 📚, 🤯 )
9293
4. **Classes**
9394
- O básico de classes: [Definição de classe](ultimatepython/classes/basic_class.py) ( 🍰 )

README.zh_tw.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ print("Ultimate Python 學習大綱")
8585
- 雙端隊列:[deque](ultimatepython/data_structures/deque.py) ( 🤯 )
8686
- Namedtuple: [namedtuple](ultimatepython/data_structures/namedtuple.py) ( 🤯 )
8787
- Defaultdict: [defaultdict](ultimatepython/data_structures/defaultdict.py) ( 🤯 )
88+
- 迭代器工具:[迭代器工具](ultimatepython/data_structures/itertools.py) ( 🤯 )
8889
- 時間複雜度:[cPython操作](https://wiki.python.org/moin/TimeComplexity) ( 📚, 🤯 )
8990
4. **類別**
9091
- 基本類別:[基本定義](ultimatepython/classes/basic_class.py) ( 🍰 )
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
Itertools provides a collection of tools for handling iterators. This
3+
module demonstrates how to use itertools functions to efficiently work
4+
with sequences, combine iterables, and create infinite iterators.
5+
"""
6+
7+
import itertools
8+
9+
10+
def main() -> None:
11+
# chain() combines multiple iterables into a single iterator
12+
letters = ["a", "b", "c"]
13+
numbers = [1, 2, 3]
14+
combined = list(itertools.chain(letters, numbers))
15+
assert combined == ["a", "b", "c", 1, 2, 3]
16+
17+
# cycle() creates an infinite iterator that cycles through the elements
18+
# We'll use islice to take only the first few elements
19+
cycled = list(itertools.islice(itertools.cycle(["A", "B"]), 6))
20+
assert cycled == ["A", "B", "A", "B", "A", "B"]
21+
22+
# repeat() creates an iterator that repeats a value infinitely
23+
repeated = list(itertools.islice(itertools.repeat("hello"), 4))
24+
assert repeated == ["hello", "hello", "hello", "hello"]
25+
26+
# count() creates an infinite iterator that counts up from a start value
27+
counted = list(itertools.islice(itertools.count(10), 5))
28+
assert counted == [10, 11, 12, 13, 14]
29+
30+
# islice() allows slicing of iterators (similar to list slicing)
31+
data = itertools.count() # infinite count from 0
32+
sliced = list(itertools.islice(data, 2, 8, 2)) # start=2, stop=8, step=2
33+
assert sliced == [2, 4, 6]
34+
35+
# tee() creates multiple independent iterators from one
36+
original = iter([1, 2, 3, 4, 5])
37+
iter1, iter2 = itertools.tee(original, 2)
38+
list1 = list(iter1)
39+
list2 = list(iter2)
40+
assert list1 == [1, 2, 3, 4, 5]
41+
assert list2 == [1, 2, 3, 4, 5]
42+
43+
# groupby() groups consecutive equal elements
44+
group_data = [1, 1, 2, 2, 2, 3, 1, 1]
45+
groups = [(key, list(group)) for key, group in itertools.groupby(group_data)]
46+
assert groups == [(1, [1, 1]), (2, [2, 2, 2]), (3, [3]), (1, [1, 1])]
47+
48+
# product() creates cartesian product of input iterables
49+
colors = ["red", "blue"]
50+
sizes = ["S", "M"]
51+
combinations = list(itertools.product(colors, sizes))
52+
assert combinations == [("red", "S"), ("red", "M"), ("blue", "S"), ("blue", "M")]
53+
54+
# permutations() generates all possible orderings
55+
perms = list(itertools.permutations([1, 2, 3], 2)) # length 2 permutations
56+
assert len(perms) == 6 # 3! / (3-2)! = 6
57+
assert (1, 2) in perms
58+
assert (2, 1) in perms
59+
60+
# combinations() generates combinations (order doesn't matter)
61+
combos = list(itertools.combinations([1, 2, 3, 4], 2))
62+
assert len(combos) == 6 # C(4,2) = 6
63+
assert (1, 2) in combos
64+
assert (2, 1) not in combos # order doesn't matter
65+
66+
67+
if __name__ == "__main__":
68+
main()

0 commit comments

Comments
 (0)