Skip to content

Commit f6a8f58

Browse files
committed
Add flatten example
1 parent 6e62a8e commit f6a8f58

2 files changed

Lines changed: 164 additions & 0 deletions

File tree

source_code/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ certain subtle points.
3434
longest chain.
3535
1. `knights_tour.ipynb`: example of using backtracking to solve the knight's tour
3636
problem.
37+
1. `flatten.ipynb`: `itertools.chain` may not flatten a data structure as you might
38+
expect or want.

source_code/flatten.ipynb

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 3,
6+
"id": "82480372-c0ad-440c-a076-5e8cc6666e48",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"import itertools"
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"id": "2e8c295a-92e9-4ee5-aefa-5ebe658ebc04",
16+
"metadata": {},
17+
"source": [
18+
"Although `itertools.chain` is very useful, it may not do what you intend when elements are strings. It also doesn't recurse beyond the first level."
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 8,
24+
"id": "5f6e1c01-98e4-4093-984b-2f8b470c804b",
25+
"metadata": {},
26+
"outputs": [],
27+
"source": [
28+
"data = [('a', 'b'), 'c012', ('d', 'e490'), 'f', 'g', (1, (3, 7))]"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 9,
34+
"id": "96e0ea2e-28cc-438b-9e5d-e98edc521883",
35+
"metadata": {},
36+
"outputs": [
37+
{
38+
"name": "stdout",
39+
"output_type": "stream",
40+
"text": [
41+
"a\n",
42+
"b\n",
43+
"c\n",
44+
"0\n",
45+
"1\n",
46+
"2\n",
47+
"d\n",
48+
"e490\n",
49+
"f\n",
50+
"g\n",
51+
"1\n",
52+
"(3, 7)\n"
53+
]
54+
}
55+
],
56+
"source": [
57+
"for item in itertools.chain(*data):\n",
58+
" print(item)"
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"id": "308fe429-20ae-41b6-832d-ee840c1a1bb6",
64+
"metadata": {},
65+
"source": [
66+
"The following function may be more what you expect."
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": 10,
72+
"id": "b36d51df-47b6-48a6-b79e-778b5e88e17e",
73+
"metadata": {},
74+
"outputs": [],
75+
"source": [
76+
"def flatten(data, recurse=True):\n",
77+
" for item in data:\n",
78+
" if hasattr(item, '__iter__') and not isinstance(item, str):\n",
79+
" for subitem in flatten(item) if recurse else item:\n",
80+
" yield subitem\n",
81+
" else:\n",
82+
" yield item"
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": 11,
88+
"id": "64a64ff7-8b26-462a-b98b-c4c2c69b9014",
89+
"metadata": {},
90+
"outputs": [
91+
{
92+
"name": "stdout",
93+
"output_type": "stream",
94+
"text": [
95+
"a\n",
96+
"b\n",
97+
"c012\n",
98+
"d\n",
99+
"e490\n",
100+
"f\n",
101+
"g\n",
102+
"1\n",
103+
"3\n",
104+
"7\n"
105+
]
106+
}
107+
],
108+
"source": [
109+
"for item in flatten(data):\n",
110+
" print(item)"
111+
]
112+
},
113+
{
114+
"cell_type": "code",
115+
"execution_count": 12,
116+
"id": "bbb96b04-1e84-4c6d-bd8d-18638732f32e",
117+
"metadata": {},
118+
"outputs": [
119+
{
120+
"name": "stdout",
121+
"output_type": "stream",
122+
"text": [
123+
"a\n",
124+
"b\n",
125+
"c012\n",
126+
"d\n",
127+
"e490\n",
128+
"f\n",
129+
"g\n",
130+
"1\n",
131+
"(3, 7)\n"
132+
]
133+
}
134+
],
135+
"source": [
136+
"for item in flatten(data, recurse=False):\n",
137+
" print(item)"
138+
]
139+
}
140+
],
141+
"metadata": {
142+
"kernelspec": {
143+
"display_name": "Python 3 (ipykernel)",
144+
"language": "python",
145+
"name": "python3"
146+
},
147+
"language_info": {
148+
"codemirror_mode": {
149+
"name": "ipython",
150+
"version": 3
151+
},
152+
"file_extension": ".py",
153+
"mimetype": "text/x-python",
154+
"name": "python",
155+
"nbconvert_exporter": "python",
156+
"pygments_lexer": "ipython3",
157+
"version": "3.11.3"
158+
}
159+
},
160+
"nbformat": 4,
161+
"nbformat_minor": 5
162+
}

0 commit comments

Comments
 (0)