Spaces:
Sleeping
Sleeping
Add advanced collections interactive notebook
Browse files
Python/phase_3/advanced_collections.py
ADDED
@@ -0,0 +1,211 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# /// script
|
2 |
+
# requires-python = ">=3.10"
|
3 |
+
# dependencies = [
|
4 |
+
# "marimo",
|
5 |
+
# ]
|
6 |
+
# ///
|
7 |
+
|
8 |
+
import marimo
|
9 |
+
|
10 |
+
__generated_with = "0.10.14"
|
11 |
+
app = marimo.App()
|
12 |
+
|
13 |
+
|
14 |
+
@app.cell
|
15 |
+
def _():
|
16 |
+
import marimo as mo
|
17 |
+
return (mo,)
|
18 |
+
|
19 |
+
|
20 |
+
@app.cell(hide_code=True)
|
21 |
+
def _(mo):
|
22 |
+
mo.md(
|
23 |
+
"""
|
24 |
+
# 🔄 Advanced Collections in Python
|
25 |
+
|
26 |
+
Let's dive deep into advanced collection handling in Python!
|
27 |
+
|
28 |
+
## Lists of Dictionaries
|
29 |
+
A common pattern in data handling is working with lists of dictionaries -
|
30 |
+
perfect for representing structured data like records or entries.
|
31 |
+
"""
|
32 |
+
)
|
33 |
+
return
|
34 |
+
|
35 |
+
|
36 |
+
@app.cell
|
37 |
+
def _():
|
38 |
+
# Sample data: List of user records
|
39 |
+
users_data = [
|
40 |
+
{"id": 1, "name": "Alice", "skills": ["Python", "SQL"]},
|
41 |
+
{"id": 2, "name": "Bob", "skills": ["JavaScript", "HTML"]},
|
42 |
+
{"id": 3, "name": "Charlie", "skills": ["Python", "Java"]}
|
43 |
+
]
|
44 |
+
return (users_data,)
|
45 |
+
|
46 |
+
|
47 |
+
@app.cell(hide_code=True)
|
48 |
+
def _(mo):
|
49 |
+
mo.md(
|
50 |
+
"""
|
51 |
+
## Working with Lists of Dictionaries
|
52 |
+
|
53 |
+
Let's explore common operations on structured data.
|
54 |
+
Try modifying the `users_data` above and see how the results change!
|
55 |
+
"""
|
56 |
+
)
|
57 |
+
return
|
58 |
+
|
59 |
+
|
60 |
+
@app.cell
|
61 |
+
def _(users_data):
|
62 |
+
# Finding users with specific skills
|
63 |
+
python_users = [user["name"] for user in users_data if "Python" in user["skills"]]
|
64 |
+
print("Python developers:", python_users)
|
65 |
+
return (python_users,)
|
66 |
+
|
67 |
+
|
68 |
+
@app.cell(hide_code=True)
|
69 |
+
def _(mo):
|
70 |
+
mo.md("""
|
71 |
+
## Nested Data Structures
|
72 |
+
|
73 |
+
Python collections can be nested in various ways to represent complex data:
|
74 |
+
""")
|
75 |
+
return
|
76 |
+
|
77 |
+
|
78 |
+
@app.cell
|
79 |
+
def _():
|
80 |
+
# Complex nested structure
|
81 |
+
project_data = {
|
82 |
+
"web_app": {
|
83 |
+
"frontend": ["HTML", "CSS", "React"],
|
84 |
+
"backend": {
|
85 |
+
"languages": ["Python", "Node.js"],
|
86 |
+
"databases": ["MongoDB", "PostgreSQL"]
|
87 |
+
}
|
88 |
+
},
|
89 |
+
"mobile_app": {
|
90 |
+
"platforms": ["iOS", "Android"],
|
91 |
+
"technologies": {
|
92 |
+
"iOS": ["Swift", "SwiftUI"],
|
93 |
+
"Android": ["Kotlin", "Jetpack Compose"]
|
94 |
+
}
|
95 |
+
}
|
96 |
+
}
|
97 |
+
return (project_data,)
|
98 |
+
|
99 |
+
|
100 |
+
@app.cell
|
101 |
+
def _(project_data):
|
102 |
+
# Nested data accessing
|
103 |
+
backend_langs = project_data["web_app"]["backend"]["languages"]
|
104 |
+
print("Backend languages:", backend_langs)
|
105 |
+
|
106 |
+
ios_tech = project_data["mobile_app"]["technologies"]["iOS"]
|
107 |
+
print("iOS technologies:", ios_tech)
|
108 |
+
return backend_langs, ios_tech
|
109 |
+
|
110 |
+
|
111 |
+
@app.cell(hide_code=True)
|
112 |
+
def _(mo):
|
113 |
+
mo.md("""
|
114 |
+
## Data Transformation
|
115 |
+
|
116 |
+
Let's explore how to transform and reshape collection data:
|
117 |
+
""")
|
118 |
+
return
|
119 |
+
|
120 |
+
|
121 |
+
@app.cell
|
122 |
+
def _():
|
123 |
+
# Data-sample for transformation
|
124 |
+
sales_data = [
|
125 |
+
{"date": "2024-01", "product": "A", "units": 100},
|
126 |
+
{"date": "2024-01", "product": "B", "units": 150},
|
127 |
+
{"date": "2024-02", "product": "A", "units": 120},
|
128 |
+
{"date": "2024-02", "product": "B", "units": 130}
|
129 |
+
]
|
130 |
+
return (sales_data,)
|
131 |
+
|
132 |
+
|
133 |
+
@app.cell
|
134 |
+
def _(sales_data):
|
135 |
+
# Transform to product-based structure
|
136 |
+
product_sales = {}
|
137 |
+
for sale in sales_data:
|
138 |
+
if sale["product"] not in product_sales:
|
139 |
+
product_sales[sale["product"]] = []
|
140 |
+
product_sales[sale["product"]].append({
|
141 |
+
"date": sale["date"],
|
142 |
+
"units": sale["units"]
|
143 |
+
})
|
144 |
+
|
145 |
+
print("Sales by product:", product_sales)
|
146 |
+
return product_sales, sale
|
147 |
+
|
148 |
+
|
149 |
+
@app.cell(hide_code=True)
|
150 |
+
def _(mo):
|
151 |
+
mo.md("""
|
152 |
+
## Collection Utilities
|
153 |
+
|
154 |
+
Python's collections module provides specialized container datatypes:
|
155 |
+
|
156 |
+
```python
|
157 |
+
from collections import defaultdict, Counter, deque
|
158 |
+
|
159 |
+
# defaultdict - dictionary with default factory
|
160 |
+
word_count = defaultdict(int)
|
161 |
+
for word in words:
|
162 |
+
word_count[word] += 1
|
163 |
+
|
164 |
+
# Counter - count hashable objects
|
165 |
+
colors = Counter(['red', 'blue', 'red', 'green', 'blue', 'blue'])
|
166 |
+
print(colors.most_common(2)) # Top 2 most common colors
|
167 |
+
|
168 |
+
# deque - double-ended queue
|
169 |
+
history = deque(maxlen=10) # Only keeps last 10 items
|
170 |
+
history.append(item)
|
171 |
+
```
|
172 |
+
""")
|
173 |
+
return
|
174 |
+
|
175 |
+
|
176 |
+
@app.cell
|
177 |
+
def _():
|
178 |
+
from collections import Counter
|
179 |
+
|
180 |
+
# Example using Counter
|
181 |
+
programming_languages = [
|
182 |
+
"Python", "JavaScript", "Python", "Java",
|
183 |
+
"Python", "JavaScript", "C++", "Java"
|
184 |
+
]
|
185 |
+
|
186 |
+
language_count = Counter(programming_languages)
|
187 |
+
print("Language frequency:", dict(language_count))
|
188 |
+
print("Most common language:", language_count.most_common(1))
|
189 |
+
return Counter, language_count, programming_languages
|
190 |
+
|
191 |
+
|
192 |
+
@app.cell(hide_code=True)
|
193 |
+
def _(mo):
|
194 |
+
callout_text = mo.md("""
|
195 |
+
## Level Up Your Collections!
|
196 |
+
|
197 |
+
Next Steps:
|
198 |
+
|
199 |
+
- Practice transforming complex data structures
|
200 |
+
- Experiment with different collection types
|
201 |
+
- Try combining multiple data structures
|
202 |
+
|
203 |
+
Keep organizing! 📊✨
|
204 |
+
""")
|
205 |
+
|
206 |
+
mo.callout(callout_text, kind="success")
|
207 |
+
return (callout_text,)
|
208 |
+
|
209 |
+
|
210 |
+
if __name__ == "__main__":
|
211 |
+
app.run()
|