tag:crieit.net,2005:https://crieit.net/tags/Pytthon/feed 「Pytthon」の記事 - Crieit Crieitでタグ「Pytthon」に投稿された最近の記事 2021-03-27T08:33:28+09:00 https://crieit.net/tags/Pytthon/feed tag:crieit.net,2005:PublicArticle/16774 2021-03-27T08:33:28+09:00 2021-03-27T08:33:28+09:00 https://crieit.net/posts/python-built-in-types-list Python リスト型 list <p>Pythonのリスト型listは基本的なシーケンス型の1つでミュータブル(作成後に変更できるオブジェクト)です。</p> <h2 id="リストの定義"><a href="#%E3%83%AA%E3%82%B9%E3%83%88%E3%81%AE%E5%AE%9A%E7%BE%A9">リストの定義</a></h2> <p>コンマ区切で要素を並べ、角括弧で囲む他、<br /> 内包表記や<code>list</code>を使って定義できます。</p> <pre><code class="python">list_sample = [] list_sample = ['a', 'b', 'c'] list_sample = [x for x in 'xyz'] list_sample = list([1, 2, 3]) </code></pre> <h2 id="リストの入れ子"><a href="#%E3%83%AA%E3%82%B9%E3%83%88%E3%81%AE%E5%85%A5%E3%82%8C%E5%AD%90">リストの入れ子</a></h2> <pre><code class="python">m = [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']] print(m[1]) # => ['D', 'E', 'F']] print(m[1][2]) # => F </code></pre> <h2 id="リスト情報の取得"><a href="#%E3%83%AA%E3%82%B9%E3%83%88%E6%83%85%E5%A0%B1%E3%81%AE%E5%8F%96%E5%BE%97">リスト情報の取得</a></h2> <h3 id="リストの要素へのアクセス"><a href="#%E3%83%AA%E3%82%B9%E3%83%88%E3%81%AE%E8%A6%81%E7%B4%A0%E3%81%B8%E3%81%AE%E3%82%A2%E3%82%AF%E3%82%BB%E3%82%B9">リストの要素へのアクセス</a></h3> <p>インデックスやスライスでのアクセスが可能です。</p> <pre><code class="python">list_sample = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] list_sample[0] # => a list_sample[1] # => b list_sample[-1] # => j list_sample[2:5] # => ['c', 'd', 'e'] list_sample[3:9:2] # => ['d', 'f', 'h'] list_sample[:] # => ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] </code></pre> <h3 id="リストの長さ"><a href="#%E3%83%AA%E3%82%B9%E3%83%88%E3%81%AE%E9%95%B7%E3%81%95">リストの長さ</a></h3> <pre><code class="python">list_sample = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] print(len(list_sample)) # => 10 </code></pre> <h3 id="回数カウント"><a href="#%E5%9B%9E%E6%95%B0%E3%82%AB%E3%82%A6%E3%83%B3%E3%83%88">回数カウント</a></h3> <pre><code class="python">list_sample = ['a', 'b', 'a', 'c', 'a', 'c'] list_sample.count('a') # => 3 </code></pre> <h3 id="要素の存在判定"><a href="#%E8%A6%81%E7%B4%A0%E3%81%AE%E5%AD%98%E5%9C%A8%E5%88%A4%E5%AE%9A">要素の存在判定</a></h3> <pre><code class="python">list_sample = ['a', 'b', 'c'] 'a' in list_sample # => True 'X' not in list_sample # => True </code></pre> <h3 id="最大最小"><a href="#%E6%9C%80%E5%A4%A7%E6%9C%80%E5%B0%8F">最大最小</a></h3> <pre><code class="python">list_sample = [1, 5, 2, 4] max(list_sample) # => 5 min(list_sample) # => 1 </code></pre> <h3 id="インデックスの位置"><a href="#%E3%82%A4%E3%83%B3%E3%83%87%E3%83%83%E3%82%AF%E3%82%B9%E3%81%AE%E4%BD%8D%E7%BD%AE">インデックスの位置</a></h3> <p><code>index(対象の要素[, 開始位置[, 終了位置]])</code>で要素の位置を取得</p> <pre><code class="python">list_sample = ['a', 'b', 'a', 'b', 'c', 'a' , 'c'] list_sample.index('a') # => 0 list_sample.index('a', 1) # => 2 list_sample.index('a', 3 , 6) # => 5 </code></pre> <h2 id="リストの操作"><a href="#%E3%83%AA%E3%82%B9%E3%83%88%E3%81%AE%E6%93%8D%E4%BD%9C">リストの操作</a></h2> <h3 id="リスト要素の更新"><a href="#%E3%83%AA%E3%82%B9%E3%83%88%E8%A6%81%E7%B4%A0%E3%81%AE%E6%9B%B4%E6%96%B0">リスト要素の更新</a></h3> <pre><code class="python">list_sample = ['a', 'b', 'c'] list_sample[1] = 'X' list_sample # => ['a', 'X', 'c'] </code></pre> <h3 id="要素の追加"><a href="#%E8%A6%81%E7%B4%A0%E3%81%AE%E8%BF%BD%E5%8A%A0">要素の追加</a></h3> <pre><code class="python">list_sample = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] list_sample.append('X') print(list_sample) # => ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'X'] </code></pre> <h3 id="要素の挿入"><a href="#%E8%A6%81%E7%B4%A0%E3%81%AE%E6%8C%BF%E5%85%A5">要素の挿入</a></h3> <pre><code class="python">list_sample = ['a', 'b', 'c'] list_sample.insert(2,'X') list_sample # => ['a', 'b', 'X', 'c'] </code></pre> <h3 id="指定した値と同じ最初の要素を除去"><a href="#%E6%8C%87%E5%AE%9A%E3%81%97%E3%81%9F%E5%80%A4%E3%81%A8%E5%90%8C%E3%81%98%E6%9C%80%E5%88%9D%E3%81%AE%E8%A6%81%E7%B4%A0%E3%82%92%E9%99%A4%E5%8E%BB">指定した値と同じ最初の要素を除去</a></h3> <pre><code class="python">list_sample = ['a', 'b', 'c', 'b', 'a'] list_sample.remove('b') list_sample # => ['a', 'c', 'b', 'a'] </code></pre> <h3 id="指定位置の要素を取り出す"><a href="#%E6%8C%87%E5%AE%9A%E4%BD%8D%E7%BD%AE%E3%81%AE%E8%A6%81%E7%B4%A0%E3%82%92%E5%8F%96%E3%82%8A%E5%87%BA%E3%81%99">指定位置の要素を取り出す</a></h3> <pre><code class="python">list_sample = ['a', 'b', 'c'] list_sample.pop(1) # => 'b' list_sample # => ['a', 'c'] </code></pre> <h3 id="delによる削除"><a href="#del%E3%81%AB%E3%82%88%E3%82%8B%E5%89%8A%E9%99%A4">delによる削除</a></h3> <pre><code class="python">list_sample = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] del list_sample[3:5] list_sample # => ['a', 'b', 'c', 'f', 'g', 'h', 'i', 'j'] del list_sample[2:7:2] list_sample # => ['a', 'b', 'f', 'h', 'j'] </code></pre> <h3 id="要素のクリア"><a href="#%E8%A6%81%E7%B4%A0%E3%81%AE%E3%82%AF%E3%83%AA%E3%82%A2">要素のクリア</a></h3> <pre><code class="python">list_sample = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] list_sample.clear() list_sample # => [] </code></pre> <h3 id="リストのコピー"><a href="#%E3%83%AA%E3%82%B9%E3%83%88%E3%81%AE%E3%82%B3%E3%83%94%E3%83%BC">リストのコピー</a></h3> <pre><code class="python">list_sample = ['a', 'b', 'c'] list_copy = list_sample.copy() list_copy # => ['a', 'b', 'c'] </code></pre> <h3 id="スライスによるリストの操作"><a href="#%E3%82%B9%E3%83%A9%E3%82%A4%E3%82%B9%E3%81%AB%E3%82%88%E3%82%8B%E3%83%AA%E3%82%B9%E3%83%88%E3%81%AE%E6%93%8D%E4%BD%9C">スライスによるリストの操作</a></h3> <pre><code class="python">list_sample = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] list_sample[2:4] = ['1', '2', '3'] print(list_sample) # => ['a', 'b', '1', '2', '3', 'e', 'f', 'g', 'h', 'i', 'j'] list_sample[3:] = [] print(list_sample) # => ['a', 'b', '1'] </code></pre> <pre><code class="python">list_sample = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] list_sample[2:7:2] = ['X', 'Y', 'Z'] list_sample # => ['a', 'b', 'X', 'd', 'Y', 'f', 'Z', 'h', 'i', 'j'] </code></pre> <h3 id="ソート"><a href="#%E3%82%BD%E3%83%BC%E3%83%88">ソート</a></h3> <pre><code class="python">list_sample = ['a', 'c', 'A', 'b', 'C', 'B' ] list_sample.sort() list_sample # => ['A', 'B', 'C', 'a', 'b', 'c'] list_sample.sort(key=str.lower) list_sample # => ['a', 'A', 'b', 'B', 'c', 'C'] list_sample.sort(key=str.lower, reverse=True) list_sample # => ['c', 'C', 'b', 'B', 'a', 'A'] </code></pre> <h3 id="順序逆転、逆順"><a href="#%E9%A0%86%E5%BA%8F%E9%80%86%E8%BB%A2%E3%80%81%E9%80%86%E9%A0%86">順序逆転、逆順</a></h3> <pre><code class="python">list_sample = ['a', 'b', 'c'] list_sample.reverse() list_sample # => ['c', 'b', 'a'] </code></pre> <h3 id="リストの繰り返し"><a href="#%E3%83%AA%E3%82%B9%E3%83%88%E3%81%AE%E7%B9%B0%E3%82%8A%E8%BF%94%E3%81%97">リストの繰り返し</a></h3> <pre><code class="python">list_sample = [1, 2] list_sample * 3 #=> [1, 2, 1, 2, 1, 2] list_sample *= 5 #=> [1, 2, 1, 2, 1, 2, 1, 2, 1, 2] </code></pre> <h3 id="リストの連結"><a href="#%E3%83%AA%E3%82%B9%E3%83%88%E3%81%AE%E9%80%A3%E7%B5%90">リストの連結</a></h3> <p><code>+</code>で末尾にリスト追加する。</p> <pre><code class="python">list_sample = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] list_sample + ['1','2', '3'] # =&gt; ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', '1', '2', '3'] </code></pre> <pre><code class="python">list_sample = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] list_sample.extend(['1','2', '3']) list_sample # => ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', '1', '2', '3'] </code></pre> maru3kaku4kaku