python3

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

投稿日:2022年7月28日 更新日:

next() を使う。

my_arr = ['春','夏','秋','冬']
my_iter = iter(my_arr)
print(next(my_iter,'なし'))
print(next(my_iter,'なし'))
print(next(my_iter,'なし'))
print(next(my_iter,'なし'))
print(next(my_iter,'なし'))

結果

春
夏
秋
冬
なし

-python3
-

執筆者:


comment

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

関連記事

no image

python3 で、配列の最大値のインデックスを1つ求める方法

配列の index() メソッドを使うと、その値のインデックスを求めることができる。 最大値を求めるメソッド max と組み合わせて使う。 例 arr = [2,4,5,10,8,-3] in …

no image

python3 でリストの要素を None に変更する方法

要素を None に変更すればよい。 例 arr = [1,2,3,4,5] print(str(arr)) for i in range(len(arr)): arr[i] = No …

no image

matplotlib でグラフの中に注釈の文字を書く方法

annotate を使う。 例 import numpy as np from matplotlib import pyplot as plt x = np.linspace(-10,10,100) …

no image

matplotlib で散布グラフを描く方法

pyplt.scatter を使って散布グラフを描くことができる。 点の色は、点ごとに変えることができる。 下の例では color の配列で点の色を指定している。 例 from matplotlib …

no image

matplotlib で、y軸に二種類の軸を設定する方法

y軸に、2種類のグラフを異なるスケールでプロットする。 matplotlib axes の twinx() を使う。 https://matplotlib.org/stable/api/_as_gen …