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