Call a function on every element within a nested container >>> def inc(x): ... return x + 1 >>> L = [[1, 2], [3, 4, 5]] >>> ndeepmap(2, inc, L) [[2, 3], [4, 5, 6]]
(n, func, seq)
| 301 | |
| 302 | |
| 303 | def ndeepmap(n, func, seq): |
| 304 | """Call a function on every element within a nested container |
| 305 | |
| 306 | >>> def inc(x): |
| 307 | ... return x + 1 |
| 308 | >>> L = [[1, 2], [3, 4, 5]] |
| 309 | >>> ndeepmap(2, inc, L) |
| 310 | [[2, 3], [4, 5, 6]] |
| 311 | """ |
| 312 | if n == 1: |
| 313 | return [func(item) for item in seq] |
| 314 | elif n > 1: |
| 315 | return [ndeepmap(n - 1, func, item) for item in seq] |
| 316 | elif isinstance(seq, list): |
| 317 | return func(seq[0]) |
| 318 | else: |
| 319 | return func(seq) |
| 320 | |
| 321 | |
| 322 | def import_required(mod_name, error_msg): |