The operation of the dictionary in Python Zz__python

Source: Internet
Author: User
Tags in python

Dictionaries

the dictionary in Python, like the HashMap in Java, exists and operates in the form of key-value pairs, which are characterized by access through keys, not offsets; key-value pairs are unordered; keys and values can be arbitrary; length variable, arbitrary nested; In the dictionary, no more sequence operations, although the dictionary is similar to the list in some ways, but do not set the list in the dictionary

 1 Basic Operation python code strong>>>> table = {' abc ':1,  ' def ':2,  ' Ghi ': 3}    >>>  table[' abc ']    1    >>> len (table)    3    >>>  Table.keys ()    [' abc ',  ' Ghi ',  ' def ']    >>> table.values ()     [1, 3, 2]    >>> table.has_key (' def ')    true   > >> table.items ()    [(' abc ',  1),  (' Ghi ',  3),  (' Def ',  2)]   

2) Modify, delete, add python code >>> table = {' abc ': 1, ' Def ': 2, ' Ghi ': 3} >>> t able[' ghi '] = (' g ', ' h ', ' I ') >>> table {' abc ': 1, ' Ghi ': (' g ', ' h ', ' I '), ' Def ': 2} >>> del ta ble[' abc '] >>> table {' Ghi ': (' g ', ' h ', ' I '), ' Def ': 2} >>> table[' xyz '] = [' x ', ' y ', ' Z '] & gt;>> Table {' xyz ': [' x ', ' y ', ' z '], ' ghi ': (' g ', ' h ', ' I '), ' Def ': 2}

here, for the dictionary expansion, just define a new key-value pair, and for the list, you can only use the Append method or fragment assignment.

3) Traversal of the dictionary python code >>> table = {' abc ':1,  ' def ':2,  ' Ghi ': 3}    >>>  for key in table.keys ():        print key,  '/t ',  table[key]               abc     1    ghi     3    def     2  

Related Article

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.