Python の配列には push_front は無くて + で配列同士を連結する ― 2021年05月27日 12時38分30秒
配列を使っていたのだが、先頭に新しい要素を追加したい。std::vector や std::deque の push_front() を探したが見付からなかった。C++ だと、今あるコンテナを壊さずにと考えてしまうが、Python では業々 push_front 等を作らずに配列を足してしまえば良いとの考え方だ。
一応、実行結果。
#!/usr/local/bin/python
numbers = [ 1, 2, 3, 4, 5 ]
print( numbers )
numbers = [ 6 ] + numbers
print( numbers )
% python3.7 push_front.py
[1, 2, 3, 4, 5]
[6, 1, 2, 3, 4, 5]
最近のコメント