LoginSignup
0
0

More than 1 year has passed since last update.

Python リストが1次元か多次元か確認するときに便利なメソッド

Posted at

はじめに

リストの次元数によって処理を分岐しようとしたときに調べました

主に1次元リストを想定しているが、まれに2次元リストの処理もしたいときにどうすべきか考えて

方法

def how_dimensions(something_list):
    if not isinstance(something_list[0],list):
        print('1次元リスト')
    else:
        print('多次元リスト')

ls1 = [1,2,3]
ls2 = [ls1,ls1]

how_dimensions(ls1)
how_dimensions(ls2)
1次元リスト
多次元リスト
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