未分類

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

投稿日:

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

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

結果

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

-未分類

執筆者:


comment

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

関連記事

no image

tex で同値記号(⇔)を入力する方法

同値記号を入力するには、\Leftrightarrow を使う。 例 $b \leftrightarrow c$, $b \Leftrightarrow c$

no image

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

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

no image

python3 で文字列の最後の文字を削除する方法

文字列の最後に [:-1] を付けると、最後の文字を削除することができる。 例 res = ‘あいうえお'[:-1] print(res) 結果 あいうえ

no image

c言語で文字をつなげる方法

sprintf 関数を使って、result 変数に文字列 s1 と文字列 s2 を結合した文字列を出力することができる。 例 #include<stdio.h> int main(void) { …

no image

tex で打ち消し線を入力する方法

次のように入力すればよい。打ち消し線は、英語で strikethrough と呼ぶ。 例 \usepackage[normalem]{ulem} \begin{document} Good \sout …