Python multidimensional array

Even though I use a multidimensional array, it is troublesome to write in list comprehension notation, and it is not readable.

def mlist(n, *args, **keys):
    if len(args) == 0:
        return [keys.get('default')] * n
    else:
        return [mlist(*args, **keys) for i in range(n)]

print mlist(5)
# [None, None, None, None, None]

print mlist(2, 3, default='a')
# [['a', 'a', 'a'], ['a', 'a', 'a']]

print mlist(4, 3, 2, default=1)
# [[[1, 1], [1, 1], [1, 1]], [[1, 1], [1, 1], [1, 1]], [[1, 1], [1, 1], [1, 1]], [[1, 1], [1, 1], [1, 1]]]

Recommended Posts

Python multidimensional array
Empty multidimensional array in python
[Beginner] Python array
Python array basics
python numpy array calculation
[python] Array slice operation
Python memo (for myself): Array
Quicksort an array in Python 3
Python
Python: 3D array image (numpy.array)
Stumble story with Python array
Create a python numpy array
Multidimensional array initialization of list
[Python] Copy of multidimensional list
Multidimensional array calculation without Numpy
Python 2D array trap [Copy array]
Subscript access to python numpy array
Multidimensional array calculation without Numpy Part 2
Python application: Numpy Part 3: Double array
[Python] How to swap array values
Compare Python and JavaScript array loops
kafka python
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
python tips
python function ①
Python basics
Python memo
ufo-> python (3)
Python comprehension
install python
Python Singleton
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
[Python] Chapter 04-03 Various data structures (multidimensional list)
Python installation
python tips
Python> link> 2D array initialization and assignment
Installing Python 3.4.3.
Python / numpy> list (numpy array) file save / load
Try python
Python memo
Python iterative
Python algorithm
[Python] Summary of array generation (initialization) time! !! !!
Python2 + word2vec
[Python] Variables