私はPythonの初心者です。str.partition()
3 タプルを返す関数を考えてみましょう。このタプルの要素 0 と 2 のみに関心がある場合、そのようなタプルから特定の要素のみを選択する最良の方法は何ですか?
現在、次のいずれかを実行できます。
# Introduces "part1" variable, which is useless
(part0, part1, part2) = str.partition(' ')
または:
# Multiple calls and statements, again redundancy
part0 = str.partition(' ')[0]
part2 = str.partition(' ')[2]
私はこのようなことをしたいのですが、できません:
(part0, , part2) = str.partition(' ')
# Or:
(part0, part2) = str.partition(' ')[0, 2]