Basic 02 Basic data types for the Python Progressive Tutorial series

Source: Internet
Author: User
Tags iterable

List features:

    • An ordered set

    • Index by offset to read data

    • Nesting support

    • Variable type


  1. Slice

    Forward index (left-to-right):

    >>> a = [1,2,3,4,5,6,7]
    >>> A[0:4:1] # 1 is the step
    [1, 2, 3, 4]

    Reverse index:

    >>> A[-1:-4:-1] (right-left
    )
    [7, 6, 5]
    >>>

    Default index:

    >>> a[1:]
    [2, 3, 4, 5, 6, 7]

  2. Adding actions to a list

    >>> a = [1,2,3,4]
    >>> B = [5,6,7,8]
    >>> A + b
    [1, 2, 3, 4, 5, 6,7,8] # Generate a new list
    >>>
    Extend: Takes a parameter and adds each element of the parameter to the original list, modifying the list in place instead of creating a new list

    >>> a = [+ +]
    >>> B = [4,5,6]
    >>>
    >>> A.extend (b)
    >>> A
    [1, 2, 3, 4, 5, 6]
    >>>

    Append: Add any object to the end of the list

    >>> a = [+ +]
    >>> A.append (4)
    >>> A
    [1, 2, 3, 4]

    >>> a.append ([3,4,5])//Insert List
    >>> A
    [1, 2, 3, 4, [3, 4, 5]]


    Insert: Inserts any object into the list to control the insertion position.
    [1, 2, 3, 4, [3, 4, 5]]
    >>>
    >>> A.insert (1, "abc")
    >>> A
    [1, ' abc ', 2, 3, 4, [3, 4, 5]

  3. Modify actions for a list

    Modifying the list itself requires only the direct assignment of the action.

    >>> a = [+ +]
    >>> a[1]= ' Test '
    >>> A
    [1, ' Test ', 3]

  4. Delete operation

    Del: We delete the element at the specified location by index

    >>> a = [1,2,3,4,4]
    >>> del A[0]
    >>> A
    [2, 3, 4, 4]

    Remove: Removes the first matching value from the specified value in the list. If it is not found, an exception will be thrown.

    >>> A
    [2, 3, 4, 4]
    >>> A.remove (4)
    >>> A
    [2, 3, 4

    Pop: Returns the last element and removes it from the list.

    >>> a = [1,2,3,4,5,6,7]
    >>> A.pop ()
    7
    >>> A
    [1, 2, 3, 4, 5, 6]

  5. List member relationships

    In no in we can determine whether an element is in the list, return a bool type, if True is in the lists, and vice versa.

    >>> a = [+ +]
    >>>
    >>> 2 in a
    True
    >>> 5 in a
    False
    >>> 5 Not in a
    True
    >>>

  6. List derivation

    1. First iterate all the contents of iterable, each iteration, put the corresponding content in the iterable into the Iter_var, and then apply the content of the Iter_var in the expression, Finally, a list is generated with the calculated value of the expression.

    than we're going to generate a list containing 1 to 10

    >>> [x  for x in range (1,11)]
    [1, 2, 3, 4, 5, 6, 7, 8, 9, ten]
    >>
    2. Added the judgment statement, only satisfies the condition content to put in the iterable the corresponding content in the Iter_var, then applies the content of the Iter_var in the expression, and finally generates a list with the calculated value of the expression.

    to generate all the odd list of numbers from 1 to 10.

    Range (1,11,2)
    >>> Range (1,11,2)
    [1, 3, 5, 7, 9]
    >>>

  7. Sort Flip: Sort,reverse

    Sort: Modifies the original list directly, and its return value is None

    >>> a = [33,11,22,44]
    >>> B = A.sort ()//none
    >>>
    >>> b
    >>> if B is None:
    ... print ' haha '
    >>> if B is None:
    ... print ' haha '
    ...
    haha

    Reverse function: Reverses a list, and its return value is None

    >>> A
    [11, 22, 33, 44]
    >>> B = A.reverse ()
    >>> b
    >>> A//Direct view a list variable can see the effect of flipping
    [44, 33, 22, 11]
    >>>

This article is from "Never give up!" "Blog, be sure to keep this provenance http://rockyjun.blog.51cto.com/8504719/1544886

Basic 02 Basic data types for the Python Progressive Tutorial series

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.