未分類

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

投稿日:

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

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

結果

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

-未分類

執筆者:


comment

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

関連記事

no image

c++ で文字列が他の文字列を含むか判定する方法

文字列を含むかどうか判定するには、string の find を使う。見つからなかった場合は、std::string::npos を返す。 例 #include <iostream> #i …

no image

python3 で数字をゼロ埋めして表示する

string の、zfill を使う。 例 n = 3 n0 = str(n).zfill(10) print(n0) 結果 0000000003

no image

tex で脚注を表示する方法

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

no image

tex で行内を結合する方法

\multicolumn を使って、結合する行の成分の数を指定することができる。次の例では「3」を指定して、結合している。 例 \begin{table} \begin{tabular}{ |c|c| …

no image

tex で frac の意味

tex で frac コマンドは、分数(fraction)を表示するために使う。「バックスラッシュを付けた」 \frac であることに注意。 例 $\frac{2}{3}$ 結果