Python辞書と集合

16842 ワード

辞書の要素を判断するにはinまたはnot inとhasを使用します.key()関数で判断する
>>> Dict={'one':1,'tow':2}
>>> 'one' in Dict
True
>>> Dict.has_key('one')
True
>>> 'one' not in Dict
False

辞書の更新
>>> Dict['one']=11
>>> Dict['three']=33
>>> Dict
{'three': 33, 'tow': 2, 'one': 11}

辞書および辞書要素の削除
>>> Dict.pop('one')
11
>>> Dict
{'three': 33, 'tow': 2}
>>> del Dict['tow']
>>> Dict
{'three': 33}
>>> Dict['one']=11
>>> Dict.clear()
>>> Dict
{}
>>> del Dict
>>> Dict
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'Dict' is not defined

dict.pop(‘key’)とdel dict[‘key’]指定辞書エントリdict.clear()はdictを削除するすべてのエントリdel dictは辞書全体を削除する
注意:dict,list,file,bool,str,input,lenのような組み込みタイプを変数に命名しないでください.
辞書の比較
  • 比較辞書長
  • 辞書の比較キー(最初の比較)
  • 比較キーの値
  • dict()
    >>> List=['a','b','c','d']
    >>> Dict=dict(zip(List,range(4)))
    >>> Dict
    {'a': 0, 'c': 2, 'b': 1, 'd': 3}
    >>> dict(x=1,y=2)
    {'y': 2, 'x': 1}

    マッピングタイプの組み込み方法
    関数#カンスウ#
    説明
    dict.keys()
    辞書のすべてのキーを返します
    dict.values()
    辞書のすべてのキーの値を返します.
    dict.items()
    辞書のすべてのキーと値をメタグループで返します.
    dict.copy()
    辞書の浅いコピー
    dict.fromkeys(seq,val=None)
    辞書を作成し、seq要素をkey、valを辞書のすべての値にします.
    dict.get(key,default=None)
    ディクショナリkeyに対応する値を返します.defaultの値が返されていない場合は
    dict.setdefault(key,default=None)
    辞書key辞書の対応する値を返し、keyがなければ新しいkeyを作成します.
    dict.update(Dict)
    Dictのkey-valuesをdictに追加
    >>> Dict
    {'a': 0, 'c': 2, 'b': 1, 'd': 3}
    >>> Dict.keys()
    ['a', 'c', 'b', 'd']
    ########
    >>> Dict.values()
    [0, 2, 1, 3]
    #######
    >>> Dict.items()
    [('a', 0), ('c', 2), ('b', 1), ('d', 3)]
    ######
    >>> D=dict.copy(Dict)
    [('a', 0), ('c', 2), ('b', 1), ('d', 3)]
    ######
    >>> dict.fromkeys('abc')
    {'a': None, 'c': None, 'b': None}
    #########
    >>> dict.fromkeys(D)
    {'a': None, 'c': None, 'b': None, 'd': None}
    ##############
    >>> Dict.get('a',123)
    0
    >>> Dict.get('l',123)
    123
    {'a': 0, 'c': 2, 'b': 1, 'd': 3,'l':123}
    ##################
    >>> D.setdefault('l',6)
    6
    >>> D
    {'a': 0, 'c': 2, 'b': 1, 'd': 3, 'l': 6}
    #################
    >>> D2
    {'test': 666}
    >>> Dict.update(D2)
    >>> Dict
    {'a': 0, 'test': 666, 'c': 2, 'b': 1, 'd': 3}

    集合タイプ集合と他のコンテナの差は多くなく,inとnot inで集合内の要素を判断することをサポートするlen()で集合の基数を得ることができる集合は無秩序であるためインデックスやスライスで操作することはできず,keysもないがforで循環反復集合タイプで可変集合と非可変集合(frozenset)に分けることができる.
    >>> s1=set('lockeroots')
    >>> s1
    set(['c', 'e', 'k', 'l', 'o', 's', 'r', 't'])
    >>> s2=frozenset('lockeroots')
    >>> s2
    frozenset(['c', 'e', 'k', 'l', 'o', 's', 'r', 't'])

    アクセスセットの値
    >>> for i in s1:
    ...     print i
    ... 
    c
    e
    k
    l
    o
    s
    r
    t
    >>> 'l' in s1
    True
    >>> 'd' in s1
    False

    更新集合値可変集合は更新できません
    >>> s1.add('d')
    >>> 'd' in s1
    True
    >>> s1.update('abc')
    >>> s1
    set(['a', 'c', 'b', 'e', 'd', 'k', 'l', 'o', 's', 'r', 't'])
    >>> s1.remove('a')
    >>> s1
    set(['c', 'b', 'e', 'd', 'k', 'l', 'o', 's', 'r', 't'])

    集合のオペレータが結合する(|)は,2つの集合を結合することを表す.
    交差(&)2つのセットに同じ要素があります
    差分/補完(-)要素は、最初の集合にのみ属し、2番目の集合には属しません.
    対称差分(^)は、1番目の要素のみが2番目の要素に属していないことと、2番目の要素のみが1番目の要素に属していないことを示します.
    >>> s1=set('abcdefg')
    >>> s2=set('efghijk')
    >>> s1|s2
    set(['a', 'c', 'b', 'e', 'd', 'g', 'f', 'i', 'h', 'k', 'j'])
    >>> s1&s2
    set(['e', 'g', 'f'])
    >>> s1-s2
    set(['a', 'c', 'b', 'd'])
    >>> s1^s2
    set(['a', 'c', 'b', 'd', 'i', 'h', 'k', 'j'])

    集合の組み込み方法s.issubset(s 1)sがs 2のサブセットである場合はTrueを返し,そうでない場合はFalse s.issuperset(s 1)sがs 2のスーパーセットである場合はTrueを返し,そうでない場合はFalse s.copy()を返してsを浅いコピーする
    >>> s=s1|s2
    >>> s
    set(['a', 'c', 'b', 'e', 'd', 'g', 'f', 'i', 'h', 'k', 'j'])
    >>> s.issubset(s1)
    False
    >>> s.issuperset(s1)
    True
    >>> s1.union(s2)
    set(['a', 'c', 'b', 'e', 'd', 'g', 'f', 'i', 'h', 'k', 'j'])
    >>> s1.intersection(s2)
    set(['e', 'g', 'f'])
    >>> s3=s1.copy()
    >>> s3
    set(['a', 'c', 'b', 'e', 'd', 'g', 'f'])