Python Data structure-Dictionary of sequences

Source: Internet
Author: User



The dictionary is the only built-in mapping type in Python. (Mappings are data structures that refer to values by name).

The values in the dictionary are not in a special order, but are stored in a specific key. Keys can be numbers, strings, and tuples.


First, create a dictionary:

1. Create a dictionary:

Such as:

phonebook={' Zhangsan ': ' 233333 ', ' Lisi ': ' 322233 ', ' Wangwu ': ' 4323334 '}

A dictionary consists of pairs of keys and their corresponding values (also referred to as key/value pairs). In the phonebook example, the name is the key and the phone number is the value. The keys and values are separated by a colon (:), and the items are separated by commas (,). The entire dictionary is enclosed by a pair of curly braces. An empty dictionary (excluding any items) consists of two curly braces, such as {}.


2. The Dict function creates a dictionary:

2.1. The Dict function establishes a dictionary through other mappings (such as other dictionaries) or sequence pairs (keys, values).

Such as:

>>> items=[(' name ', ' Zhangsan '), (' age ', 44)]
>>> d=dict (items)
>>> D
{' Age ': *, ' name ': ' Zhangsan '}
>>>


2.2, Dict function can also be used to create a dictionary by keyword parameters;

Such as:

>>> a=dict (name= ' Lisi ', age=44)
>>> D
{' Age ': *, ' name ': ' Zhangsan '}
>>>

Note: The Dict function is not a real function, he is a type, just like list, tuple, and Str.

Second, the basic operation of the dictionary:

The basic behavior of the dictionary is similar to the sequence (sequence):

A), Len (d): Returns the number of items in D (key-value pairs);

b), D[k]: Returns the value associated to the key k;

c), D[k]=v: The value V is associated to the key k;

d), Del D[k]: Delete the key to K;

e), k in D check if there is a value in D containing the key k;

Dictionaries and Lists



Python Data structure-Dictionary of sequences

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.