1 2 3 4 5 6 |
<span class="cm-variable">alphabet</span> = [<span class="cm-string">"a"</span>, <span class="cm-string">"b"</span>, <span class="cm-string">"c"</span>, <span class="cm-string">"d"</span>, <span class="cm-string">"e"</span>] <span class="cm-keyword">del</span>(<span class="cm-variable">alphabet</span>[<span class="cm-number">3</span>:]) <span class="cm-keyword">del</span>(<span class="cm-variable">alphabet</span>[<span class="cm-number">0</span></code><code class="cm-s-ipython language-python">])</code><code class="cm-s-ipython language-python"> <span class="cm-builtin">print</span>(<span class="cm-variable">alphabet</span>) <span class="cm-comment"># ["b", "c"]が出力される </span> |
del(リスト[インデックス番号])
例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
In [9]: sample_list = ['a','b','c','d','e'] In [10]: sample_list[0] Out[10]: 'a' In [11]: sample_list[0:] Out[11]: ['a', 'b', 'c', 'd', 'e'] In [12]: sample_list[0:-1] Out[12]: ['a', 'b', 'c', 'd'] In [13]: sample_list[0:4] Out[13]: ['a', 'b', 'c', 'd'] In [19]: sample_list[-3:] Out[19]: ['c', 'd', 'e'] In [22]: sample_list[-3:-1] Out[22]: ['c', 'd'] # リストを反転させるとき In [14]: sample_list[::-1] Out[14]: ['e', 'd', 'c', 'b', 'a'] |
1 |
コメントを残す