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

matplotlib で、複数のグラフを簡単にプロットする方法

変数を並べて書くと、次のように自動的に順番を理解してグラフを表示してくれる。 例 import numpy as np from matplotlib import pyplot as plt fro …

no image

matplotlib で、凡例を表示する位置を変更する方法

凡例(legend)の表示位置を変更するには、axis の legend の loc を設定する。 次のリンクhttps://matplotlib.org/stable/api/_as_gen/mat …

no image

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

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

no image

matplotlib で、グラフの線の幅を変更する方法

lw で、線幅を数字で指定すればよい。 例 from matplotlib import pyplot as plt data_x = [25,26,27] data_y = [39, …

no image

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

リストを iter に変えたあと、… next() を使う。 参考リンク https://www.programiz.com/python-programming/methods/buil …