Python Basics (7): Basic data Type (DICT)

Source: Internet
Author: User
Tags iterable shallow copy

Dictionary (dict):

Description: A dictionary is another mutable container model and can store any type of object. Each key value of the dictionary (Key=>value) is split with a colon (:), each pair is separated by a comma (,), and the entire dictionary is included in curly braces {} . Format:d = {key1 : value1, key2 : value2 }

Example:

    Dict = {'name': ' nios', 'age': ' +', ' sex ' : 'male'}

Basic operation:

    • Index
    • New
    • Delete
    • Key, value, key-value pairs
    • Cycle
    • Length
classDict (object):"""dict (), New Empty Dictionary dict (mapping), new dictionary initialized from a mapping object ' s (key, value) pairs Dict (iterable), new dictionary initialized as if via:d = {} for K, V in Iterabl E:d[k] = v dict (**kwargs), new dictionary initialized with the name=value pairs in the keyword  Argument list. For Example:dict (one=1, two=2)"""    defClear (self):#real signature unknown; restored from __doc__        """d.clear (), None. Remove all items from D."""        Pass    defCopy (self):#real signature unknown; restored from __doc__        """d.copy (), a shallow copy of D"""        Pass@staticmethod#known case    defFromkeys (*args, **kwargs):#Real Signature Unknown        """Returns a new dict with keys from iterable and values equal to value."""        Pass    defGet (self, k, d=none):#real signature unknown; restored from __doc__        """D.get (K[,d]), D[k] if k in D, else D. D defaults to None. """        Pass    defItems (self):#real signature unknown; restored from __doc__        """D.items () a Set-like object providing a view on D ' s items"""        Pass    defKeys (self):#real signature unknown; restored from __doc__        """D.keys () a Set-like object providing a view on D ' s keys"""        Pass    defPop (self, k, d=none):#real signature unknown; restored from __doc__        """D.pop (K[,d])-V, remove specified key and return the corresponding value. If key is no found, D is returned if given, otherwise keyerror is raised"""        Pass    defPopitem (self):#real signature unknown; restored from __doc__        """D.popitem (), (k, v), remove and return some (key, value) pair as a 2-tuple; but raise keyerror if        D is empty. """        Pass    defSetDefault (self, k, d=none):#real signature unknown; restored from __doc__        """D.setdefault (K[,d]), D.get (K,d), also set D[k]=d if K not in D"""        Pass    defUpdate (self, E=none, **f):#known special case of Dict.update        """d.update ([E,]**f), None.        Update D from Dict/iterable E and F. If E is present and have a. Keys () method, then Does:for k in e:d[k] = E[k] If e is present and lacks a. Keys () m Ethod, then does:for K, v in e:d[k] = V In either case, the is followed By:for k in f:d[k] = F[k] """        Pass    defVALUES (self):#real signature unknown; restored from __doc__        """d.values () A object providing a view on D ' s values"""        Pass    def __contains__(Self, *args, **kwargs):#Real Signature Unknown        """True If D has a key k, else False."""        Pass    def __delitem__(Self, *args, **kwargs):#Real Signature Unknown        """Delete Self[key]."""        Pass    def __eq__(Self, *args, **kwargs):#Real Signature Unknown        """Return Self==value."""        Pass    def __getattribute__(Self, *args, **kwargs):#Real Signature Unknown        """Return getattr (self, name)."""        Pass    def __getitem__(Self, y):#real signature unknown; restored from __doc__        """x.__getitem__ (y) <==> X[y]"""        Pass    def __ge__(Self, *args, **kwargs):#Real Signature Unknown        """Return Self>=value."""        Pass    def __gt__(Self, *args, **kwargs):#Real Signature Unknown        """Return Self>value."""        Pass    def __init__(Self, Seq=none, **kwargs):#known special case of dict.__init__        """dict (), New Empty Dictionary dict (mapping), new dictionary initialized from a mapping object '            S (key, value) pairs Dict (iterable), new dictionary initialized as if via:d = {} For K, V in iterable:d[k] = v dict (**kwargs)-New dictionary initialized with the Name=va  Lue pairs in the keyword argument list. For Example:dict (one=1, two=2) # (copied from class Doc)"""        Pass    def __iter__(Self, *args, **kwargs):#Real Signature Unknown        """Implement iter (self)."""        Pass    def __len__(Self, *args, **kwargs):#Real Signature Unknown        """Return len (self)."""        Pass    def __le__(Self, *args, **kwargs):#Real Signature Unknown        """Return Self<=value."""        Pass    def __lt__(Self, *args, **kwargs):#Real Signature Unknown        """Return Self<value."""        Pass@staticmethod#known case of __new__    def __new__(*args, **kwargs):#Real Signature Unknown        """Create and return a new object. See Help (type) for accurate signature. """        Pass    def __ne__(Self, *args, **kwargs):#Real Signature Unknown        """Return Self!=value."""        Pass    def __repr__(Self, *args, **kwargs):#Real Signature Unknown        """Return repr (self)."""        Pass    def __setitem__(Self, *args, **kwargs):#Real Signature Unknown        """Set Self[key] to value."""        Pass    def __sizeof__(self):#real signature unknown; restored from __doc__        """d.__sizeof__ (), size of D in memory, in bytes"""        Pass    __hash__= None
Dict

Common functions:

Index Example (index value is key):

Name_dict = {'name':'Nios',' Age': 100,'Sex':'male'}Print(name_dict['name'])Print(name_dict[' Age'])Print(name_dict['Sex']) operation Result: Nios100male

For loop Example:

  

Python Basics (7): Basic data Type (DICT)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.