0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Python3エンジニア基礎試験_模擬試験の復習④

Posted at

リストの問題

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 とカウントします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?