tuple() と list[] の混在をなくす小道具。
deeplistuplify.py
def deeplistify(data):
if type(data) in (tuple, list):
return [deeplistify(d) for d in data]
return data
def deeptuplify(data):
if type(data) in (tuple, list):
return tuple([deeptuplify(d) for d in data])
return data
テスト
>>> lt = [1, 2, 3, [4, 5, [False, (6, [7, 8])], 9, 10], 11]
>>> print(deeplistify(lt))
[1, 2, 3, [4, 5, [False, [6, [7, 8]]], 9, 10], 11]
>>> print(deeptuplify(lt))
(1, 2, 3, (4, 5, (False, (6, (7, 8))), 9, 10), 11)