pythonのお勉強その4 list
listは更新可能なコレクション
[]で囲んで初期化する
>>> list = ["letitride", "-", "jp", ".", "net"] >>> print list ['letitride', '-', 'jp', '.', 'net'] >>> for i in list: ... print i ... letitride - jp . net >>> #末尾に値を追加 >>> list = [] >>> list.append("let") >>> list.append("it") >>> list.append("ride") >>> print list ['let', 'it', 'ride'] >>> #indexを指定して要素を挿入 >>> list = ["letitride","jp","."] >>> list.insert(1, "-") >>> list.insert(3, "net") >>> print list ['letitride', '-', 'jp', 'net', '.'] >>> #指定の値の要素を削除(最初に見つかった要素のみ) >>> list = ["1","2","3","2","1"] >>> list.remove("2") >>> print list ['1', '3', '2', '1'] >>> #指定要素の値を取り出し >>> list = ["1","2","3","2","1"] >>> print list.pop(1) 2 >>> print list.pop() 1 >>> print list ['1', '3', '2'] >>> #指定の値のindexを取得(最初に見つかった要素) >>> list = ["100","200","300","200","100"] >>> print list.index("200") 1 >>> #list内に含まれる指定値の件数を取得 >>> list = ["100","200","300","200","100"] >>> print list.count("200") 2 >>> #指定の値の要素すべてを削除j >>> for i in range( list.count("200")): ... list.remove("200") ... >>> print list ['100', '300', '100']