LoginSignup
0
1

More than 1 year has passed since last update.

[Python]二次元配列を平坦化する

Posted at

前提

この記事ではあくまで単純な二次元配列に限って扱っていく。
もっと多次元の配列、また階層にムラがある配列に関しては対応していない。

平坦化とは

[[1, 2], [3, 4]]

みたいな二次元配列があったとき、これを

[1, 2, 3, 4]

のように一次元にすること。

方法

itertool(外部ライブラリ) の関数を使う。
非破壊処理。

import itertools

l = [[1, 2], [3, 4]]
l = list(itertools.chain.from_iterable(l))

ここで返される値はiterableオブジェクトであるため、適宜キャストする必要がある。

0
1
2

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
1