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,'なし'))
結果
春 夏 秋 冬 なし
雑記
投稿日: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,'なし'))
春 夏 秋 冬 なし
執筆者:seyanen
関連記事
matplotlib で、複数のグラフを簡単にプロットする方法
変数を並べて書くと、次のように自動的に順番を理解してグラフを表示してくれる。 例 import numpy as np from matplotlib import pyplot as plt fro …
python3 で、pathlib を使ってファイルの拡張子を取得する方法
pathlib モジュールの stem を使う。 この書き方は、python 3.4 以降で使えるようになった。 https://docs.python.org/3/library/pathlib.h …
python3 で、csv ファイルの日付・数字を読み取る方法
csv をインポートすると、row in reader はリストになっている。 次のようにして、各行の要素を取り出して表示することができる。 例 import csv with open(‘data. …
2023/01/18
matplotlib のグラフ作成と gnuplot との対応 比較
2022/10/14
pythonで配列(リスト)の、ある要素がわかっているときにその次の要素を取得する方法。