0
0

More than 1 year has passed since last update.

小道具:([括弧])を統一する

Last updated at Posted at 2021-12-25

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)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0