Python基本文まとめ(3) 辞書・集合の使い方

自分(初心者)用のPython3の基本文法メモ
第3回目は辞書と集合について

Python3-辞書-集合

辞書(dict)

  • Pythonの連想配列(ハッシュ)のこと
  • 辞書は順番を持っていない

    辞書の書式
    { キー: アイテム, ・・・}

    アイテムを取り出す
    辞書[キー]
      >>> dic = {'赤':'red','青':'blue','緑':'green'}
      >>> dic['青']
      'blue'
      

      アイテムの追加
      辞書[キー] = アイテム
        >>> dic = {'赤':'red','青':'blue','緑':'green'}
        >>> dic['黄'] = 'yellow'
        >>> dic
        {'赤': 'red', '黄': 'yellow', '緑': 'green', '青': 'blue'}
        

        キーとアイテムの削除
        del 辞書[キー]
          >>> dic = {'赤':'red','青':'blue','緑':'green'}
          >>> del dic['青']
          >>> dic
          {'赤': 'red', '緑': 'green'}
          



          キーが存在しているかを調べる
          キー in 辞書
            >>> dic = {'赤':'red','青':'blue','緑':'green'}
            >>> '青' in dic
            True
            

            全てのキーを取得する
            辞書.keys()
              >>> dic = {'赤':'red','青':'blue','緑':'green'}
              >>> dic.keys()
              dict_keys(['赤', '緑', '青'])
              

              全てのアイテムを取得する
              辞書.values()
                >>> dic = {'赤':'red','青':'blue','緑':'green'}
                >>> dic.values()
                dict_values(['red', 'green', 'blue'])
                

                全てのキーとアイテムを取得する
                辞書.items()
                  >>> dic = {'赤':'red','青':'blue','緑':'green'}
                  >>> dic.items()
                  dict_items([('赤', 'red'), ('緑', 'green'), ('青', 'blue')])
                  

                  集合(set)

                  • 複数のデータを持つことができる
                  • データは重複できない
                  • データは順番を持っていない

                    集合の書式
                    {要素,・・・}

                    リストから集合を作る
                    set(リスト)
                      >>> fruits = ['りんご','みかん','バナナ']
                      >>> set(fruits)
                      {'みかん', 'バナナ', 'りんご'}
                      

                      要素を追加する
                      集合.add(要素)
                        >>> fruits = {'りんご','みかん','バナナ'}
                        >>> fruits.add('いちご')
                        >>> fruits
                        {'みかん', 'いちご', 'バナナ', 'りんご'}
                        

                        要素を削除する
                        集合.remove(要素)
                          >>> fruits = {'りんご','みかん','バナナ'}
                          >>> fruits.remove('みかん')
                          >>> fruits
                          {'バナナ', 'りんご'}
                          

                          全ての要素を削除する
                          集合.clear()
                            >>> fruits = {'りんご','みかん','バナナ'}
                            >>> fruits.clear()
                            >>> fruits
                            set()
                            

                            要素か集合に含まれているか調べる
                            要素 in 集合
                              >>> fruits = {'りんご','みかん','バナナ'}
                              >>> 'みかん' in fruits
                              True
                              


                              集合の演算

                              集合1と集合2に含まれる全ての要素から新しい集合を作る
                              集合1 | 集合2
                                >>> fruits1 = {'りんご','みかん','バナナ'}
                                >>> fruits2 = {'いちご','メロン','バナナ'}
                                >>> fruits1 | fruits2
                                {'メロン', 'いちご', 'バナナ', 'みかん', 'りんご'}
                                

                                集合1と集合2に共通する要素で新しい集合を作る
                                集合1 & 集合2
                                  >>> fruits1 = {'りんご','みかん','バナナ'}
                                  >>> fruits2 = {'りんご','メロン','バナナ'}
                                  >>> fruits1 & fruits2
                                  {'バナナ', 'りんご'}
                                  

                                  集合1から集合1と集合2に共通する要素を除いた要素で集合を作る
                                  集合1 - 集合2
                                    >>> fruits1 = {'りんご','みかん','バナナ'}
                                    >>> fruits2 = {'いちご','メロン','バナナ'}
                                    >>> fruits1 - fruits2
                                    {'みかん', 'りんご'}
                                    

                                    集合1と集合2のどちらか片方にだけ含まれる要素で新しい集合を作る
                                    集合1 ^ 集合2
                                      >>> fruits1 = {'りんご','みかん','バナナ'}
                                      >>> fruits2 = {'りんご','メロン','バナナ'}
                                      >>> fruits1 ^ fruits2
                                      {'メロン', 'みかん'}
                                      

                                      集合1に集合2の全ての要素が含まれるかを調べる
                                      集合1 >= 集合2
                                        fruits1 = {'りんご','みかん','バナナ'}
                                        >>> fruits2 = {'りんご','みかん'}
                                        >>> fruits1 >= fruits2
                                        True
                                        
                                        >>> fruits1 = {'りんご','みかん','バナナ'}
                                        >>> fruits2 = {'りんご','いちご'}
                                        >>> fruits1 >= fruits2
                                        False
                                        


                                        今回はここまで。
                                        次回は条件分岐について



                                        とりあえずアナコンダを入れた。
                                        けど、もっぱらiPhoneのPythonista3で勉強中


                                        0 件のコメント :