python3

python3 でランダムな整数の配列を作る方法

投稿日:2020年12月15日 更新日:

random モジュールを使う。

randint() の引数で、配列の最小値と最大値を指定できる。

import random
random_arr = [random.randint(-20,20) for _ in range(30)]
print(random_arr)

結果

[-17, 18, 9, -3, 7, -7, 14, -14, -10, 9, 3, 5, -12, -4, 18, -7, 20, 8, -18, 18, 17, 0, -20, -8, 2, -5, 16, 2, -12, 8]
[商品価格に関しましては、リンクが作成された時点と現時点で情報が変更されている場合がございます。]

ゴールデンカムイ 1【電子書籍】[ 野田サトル ]
価格:564円(税別、送料別)(2020/12/24時点)

楽天で購入

-python3
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

python3 でクラス内からだけアクセスするメソッドを作る方法

1.メソッド内でメソッドを定義する2.メソッド最初にアンダースコアをつける(慣習)例: def _some_internal_func(self): … このうち、2.のメソッド名先頭にアンダース …

no image

gnuplot で、グラフの形を正方形にする方法

set size square または、その省略形で set size sq というコマンドを使えば正方形の中にグラフを描くことができる。 例 set size square plot cos(x) …

no image

matplotlib で、x軸・y軸の目盛りを反対方向につけたい場合。

デフォルトでは、x軸はグラフの下に、y軸は左側につけられる。 例 import numpy as np from matplotlib import pyplot as plt x = np.lins …

no image

pythonで配列(リスト)の、ある要素がわかっているときにその次の要素を取得する方法。

next() を使う。 例 my_arr = [‘春’,’夏’,’秋’,’冬’] my_iter = iter(my_arr) print(next(my_iter,’なし’)) print(next …

no image

python3 で、すべて小文字かどうかを判定する方法

islower() 関数を使う。 例 print(‘abcde’.islower()) print(‘abcDe’.islower()) 結果 True False