combinations
次のコードを実行したときの出力として、正しいものを1つ選べ。
import itertools
print(list(itertools.combinations("abc", 2)))
- A
[('a', 'b'), ('b', 'a'), ('a', 'c'), ('c', 'a'), ('b', 'c'), ('c', 'b')] - B
[('a', 'b'), ('a', 'c'), ('b', 'c')] - C
[('a', 'a'), ('a', 'b'), ('b', 'b')] - D
['ab', 'ac', 'bc']
正解と解説を見る
正解:B
combinations(r) は順序を区別しない、permutations は順序を区別する、product は直積。
| 順 | 実行する行 | 実行後の変数 |
|---|---|---|
| 1 | `import itertools` | — |
| 2 | `print(list(itertools.combinations("abc", 2)))` | — |
| 結果 | 実際の出力 | [('a', 'b'), ('a', 'c'), ('b', 'c')] |
- A
誤り。順序を区別する
permutationsの結果。 - B
正しい。順序を区別しない 2 個の組合せ。
- C
誤り。重複を許す組合せの書き方ではない。
- D
誤り。要素はタプル。
このサイトの問題は、各試験の公式に示された出題範囲に沿って作ったオリジナルの模擬問題です。本試験の問題ではありません。一般社団法人Pythonエンジニア育成推進協会の公式サービスではありません。