0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonで一次元配列と二次元配列の変換(numpyなし)

Last updated at Posted at 2021-09-05

たまに忘れて1から考えるのでメモ.

sample.py
In [1]: a = [ chr(x) for x in range(65,65+16) ]

In [2]: print(a)
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P']

In [3]: b = [ a[x:x+4] for x in range(0,len(a),4) ]

In [4]: print(b)
[['A', 'B', 'C', 'D'], ['E', 'F', 'G', 'H'], ['I', 'J', 'K', 'L'], ['M', 'N', 'O', 'P']]

In [5]: from itertools import chain

In [6]: c = [ x for x in chain.from_iterable(b) ]

In [7]: print(c)
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P']

In [8]:

よくみかけるのはnumpyを使うパターンですが,このくらいの単純なリストなら組込みの範囲でできますしね.

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?