リストの問題
list.py
dive_into_code = ['Noro', 'Nakao', 'Miyaoka', 'Miyashita', 'Shibata', 'Kimura']
ex_mentor = dive_into_code.pop(3)
print(ex_mentor)
# Miyashita
● 解説
.pop() はリストからデータを取り出すメソッドです。
.pop() の引数は取り出すインデックス番号を入れています。
インデックスは一番左をゼロとしてカウントアップしていきます。
list.py
[(x, y) for x in [0,1,2] for y in [1,2,3] if x != y]
# [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 1), (2, 3)]
● 解説
リスト内包表記の問題です。for 文が 2 重になっていますが、
中の if 文で x と y が同じものは対象外にしている選択肢が正解です。
list.py
リストから、引数の値(x)の最初のアイテムを削除するメソッドを選びなさい。
# list.remove(x)
● 解説
remove() メソッドは指定した値と同じ要素を探し、最初に該当した要素を削除します。
list.py
def dive_into_code(teacher, L=[]):
L.append(teacher)
return L
print(dive_into_code('Noro'))
print(dive_into_code('Nakao'))
print(dive_into_code('Miyashita'))
# ['Noro']
# ['Noro', 'Nakao']
# ['Noro', 'Nakao', 'Miyashita']
● 解説
リストと関数の問題です。5, 6, 7 行目で関数を呼び出しています。
それぞれで実引数を用意し、teacher という仮引数に格納されます。
出力の結果を見ると仮引数の値がリストに順次格納するようになれば良いので、
.append が使われている選択肢が正解です。
list.py
num_list = [2, 4, 6, 4, 4, 2, 6]
for i in range(num_list.count(4)):
print(i, end=' ')
# 0 1 2
● 解説
.count() メソッドは、変数の中にある要素の個数を数えることができます。
ここでは 4 の個数をカウントし、3個あります。
for 文と組み合わせるとゼロ、1、 2 とカウントします。