- 2008-06-12 (木) 0:00
- Python
ソースコード
#!/usr/bin/python # coding: UTF-8 # リストの要素の追加と削除(取出し) | append()、extend()、pop()、remove()メソッドの使い方 a = [3, 5, 7, 9, 11] print a a.append(13) # 引数のオブジェクトをリストの末尾に追加 print a a.extend([15, 17, 19, 21]) # 引数のシーケンスを展開して末尾に追加 print a elm = a.pop() # リストの末尾から要素を1つ取り出す print 'elm == %d' % elm print a elm = a.pop(4) # 引数にリストのインデックスを取り、その要素を取り出す print 'elm == %d' % elm print a a.remove(13) # 引数のオブジェクトをリスト要素から削除する print a
実行結果
[3, 5, 7, 9, 11] [3, 5, 7, 9, 11, 13] [3, 5, 7, 9, 11, 13, 15, 17, 19, 21] elm == 21 [3, 5, 7, 9, 11, 13, 15, 17, 19] elm == 11 [3, 5, 7, 9, 13, 15, 17, 19] [3, 5, 7, 9, 15, 17, 19]
リファレンス
チュートリアル
関連記事
- Python: リストの抽出・連結・要素の追加
- Python: リストの初期化・出力・代入
- Python: if/for文でのin演算子の各オブジェクト毎の評価
- Python: set型の集合演算で2つのリスト要素を比較
- Python: リストの上位型であるシーケンス型の構文 - Sequence[X:Y:Z]
Sponsored Link
Comments:0
Trackbacks:0
- Trackback URL for this entry
- http://www.yukun.info/blog/2008/06/python-list2.html/trackback
- Listed below are links to weblogs that reference
- Python: リストの要素の追加と削除、取出し - append()、extend()、pop()、remove()メソッド from Yukun's Blog