未分類

python3 でリストの偶数番目・奇数番目の項目を出力する

投稿日:

リストに対して、::2 を使えば良い。

list1 = ['日','月','火','水','木','金','土']
list2 = list1[::2]
list3 = list1[1::2]
print(list2)
print(list3)

結果

['日', '火', '木', '土']
['月', '水', '金']

-未分類

執筆者:


comment

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

関連記事

no image

tex の積分範囲の分数サイズを変更する方法

定積分の分数の大きさを調整するには、\mbox のサイズ指定を \small や \normalsize や \large 等に設定すると良い。 例 $\displaystyle \int _0 ^{ …

no image

tex で脚注を表示する方法

\footnote を使う。 例 \footnote{this is a footnote.} 結果

no image

python3 で range からリストを生成する方法

list をrange を指定して初期化すれば良い。数を1つ飛ばしのリストも次のようにして作れる。 例 list1 = list(range(1,10,2)) print(list1) 結果 [1, …

no image

matplotlib で縦横の罫線を出力する

罫線を出力するには、pyplot.grid を使う。 例 import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2 …

no image

python3 で、ある文字列で始まるかどうか判定する方法

文章が、ある文字列で始まるかどうかを判定するには startswith を使う。 例 str = “こんにちは。” print(str.startswith(‘こん’)) print(str.star …