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 で変数が数値の配列かどうかチェックするメモ

Posted at

背景

Python で, 変数が数値の配列であることを ensure したい.
できれば簡潔に.
期待しないデータだった場合は, エラーか例外が出れば OK.

方法

要素ごとにチェック

l = [....]

assert all(isinstance(v, (int, float)) for v in l)

l が配列型でなければその時点で exception

numpy を使う

numpy が使える場合は,

numpy.ndarray(l, float)

でいけます! 変換できなかったら exception でます.
(l が配列であるチェックを別途入れるとなお良いか)

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?