Pythonのlistやsetについて公式ドキュメント読んで動きを確認します。
list.appendについて
list.append(x)でlistの最後の要素としてxを追加できる。
list_1 = [1, 2, 3, 4, 5] list_1.append(6) print(list_1) # [1, 2, 3, 4, 5, 6]
公式ドキュメントでAdd an item to the end of the list. Similar to a[len(a):] = [x]
と書かれていたのですがa[len(a):] = [x]がよくわからなかったので確認しました。
- len(a)でaの要素数を取得する(上記の例だと5になる)
- a[len(a):]はa[5:]と同じになり、5番目以降の要素を指定する
- 最後の列以降に[x]が入ることで、[1, 2, 3, 4, 5, 6]になる
Setsについて
A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference. (For other containers see the built-in dict, list, and tuple classes, and the collections module.)
setオブジェクトは重複を除外する配列のようなもの、ぐらいの理解はしていたのですが改めてドキュメント読んでみると「unordered」「distinct」「hashable」を再認識しました。
unordered: 出力で順序は保証されない distinct: 同じ値は一つしか入らない hashable object: これは深掘りがした方が良いので別でも調べるが、簡単に書くとsetの内部では値を高速に検索するためにhashが使われていて、listのような変更可能なオブジェクトはsetの要素にはできないとのこと
LLMで実装を自動化できるからこそ、適切にコードを直せるようにこの辺りの知識を強化していきたいと思います。