Summary of common data types in Python (iii)

Source: Internet
Author: User

4. General-Purpose sequence operation method

(1) Index

In the access sequence element, as follows:

>>> L = [' A ', ' B ', ' C ']>>> l[1] ' b ' >>> T = () >>> t[0]1 >>> str = "Python" &G t;>> str[4] ' o '

(2) sharding

Shards are used to access elements of a certain range, and shards are usually implemented by two indexes separated by colons, and are commonly seen in the following ways:

>>> A = List (range) >>> A [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> b = a[1:5]>>> b [1, 2, 3, 4]>>> C = a[-3:-1]>>> C [7, 8]>>> d = a[1:10:2]>>> d [1, 3, 5, 7, 9]

Two Mappings (dictionary)

Each element in the map has a professional name, called the key. The dictionary is the only built-in mapping type in Python, which we'll cover in more detail:

(1) Key type

A dictionary (dict) is a container that stores unordered key-value mappings (key/value) of type data that can be a key of a

Word, string, or tuple, the key must be unique. In Python, numbers, strings, and tuples are designed to be immutable types, and common lists and collections (sets) are mutable, so lists and collections cannot be keys to the dictionary. The key can be any immutable type, which is the most powerful dictionary in Python.

(2) Create

>>> d = {}>>> d[1] = 1 >>> d {1:1}>>> d[' cat '] = ' Lucy ' >>> d {1:1, ' cat ': ' Lucy '}

(3) Find

Dict is to find value by key, which represents the relationship of meaning, which can be accessed by D[key] Dict:

>>> d[' cat '] ' Lucy '

(4) Traverse

>>> d = {}>>> d[' cat '] = ' Lucy ' >>> d[' dog ' = ' Ben ' >>> for key in D:print (key + ":", D [Key])

Results

Cat:lucy

Dog:ben

(5) Advantages and disadvantages

The first feature of Dict is that the search speed is fast, and the speed of the search is independent of the number of elements, while the search speed of the list decreases with the increment of the element, and the second characteristic is that the stored key-value order pairs are not sequential; The third feature is that the element is immutable as a key, So list cannot be a key.

The disadvantage of dict is that it takes up a lot of memory, and it wastes a great deal of content.

Reprint to: (Strange _ Yang Source: http://www.cnblogs.com/ybjourney/p/4767726.html)

Summary of common data types in Python (iii)

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.