Python 2.7 Chinese tutorial and automated test Introduction (3)--to be sorted

Source: Internet
Author: User

Data


This chapter discusses in detail some of the knowledge that has been learned and introduces some new knowledge.
Detailed description of the list

All the methods of the list are as follows:

List.append (x): Attaches an element to the end of the list, equivalent to A[len (a):] = [x].
List.extend (L): the contents of the appended list L are behind the current list, which is equivalent to A[len (a):] = L.
List.insert (i, X): Inserts x at the specified position I. I means the insertion position, the original I position if there is an element that moves back one position, for example A.insert (0, X) is inserted into the list header, and A.insert (Len (a), x) is equivalent to A.append (x).
List.remove (x): Removes the first element in the list that has a value of x. If not, error.
List.pop ([i]): The output list specifies the position of the element and returns it to that element. If no index is specified for the last element. The square brackets indicate that I is optional.
List.index (x): Returns the index of the first element with a value of x. If not, then an error.
List.count (x): Returns the number of occurrences of x in the list.
List.sort (Cmp=none, Key=none, Reverse=false): Sorts the elements in the linked list in place.
List.reverse (): Reverses the element in place.

Instance:

>>> a = [66.25, 333, 333, 1, 1234.5]>>> print A.count (333), A.count (66.25), A.count (' X ') 2 1 0>>> ; A.insert (2,-1) >>> a.append (333) >>> a[66.25, 333,-1, 333, 1, 1234.5, 333]>>> a.index (333) 1& Gt;>> A.remove (333) >>> a[66.25,-1, 333, 1, 1234.5, 333]>>> a.reverse () >>> a[333, 1234.5, 1, 333,-1, 66.25]>>> a.sort () >>> a[-1, 1, 66.25, 333, 333, 1234.5]>>> A.pop () 1234.5> ;>> a[-1, 1, 66.25, 333, 333]

Use the linked list as a stack

The stack is LIFO. Use append (), out of use pop (). For example:

>>> stack = [3, 4, 5]>>> stack.append (6) >>> Stack.append (7) >>> stack[3, 4, 5, 6, 7]> ;>> Stack.pop () 7>>> stack[3, 4, 5, 6]>>> stack.pop () 6>>> stack.pop () 5>>> Stack[3, 4]

Use the list as a queue

List FIFO first. It is not efficient to insert or delete elements in a list header because other related elements need to be moved and Collections.deque is recommended.

>>> from collections import deque>>> queue = deque (["Eric ", " John ", " Michael "]) >>> queue.append (" Terry ")             # terry arrives>>> queue.append ("Graham")            # graham arrives>>> queue.popleft ()                  #  The first to arrive now leaves ' Eric ' >>> queue.popleft ()                   # The  Second to arrive now leaves ' John ' >>> queue                             # remAining queue in order of arrivaldeque ([' Michael ',  ' Terry ',  ' Graham ']) 

Functional Programming tools

For the list, there are three built-in functions that are useful: filter (), map (), and reduce ().

The filter (function, sequence) returns a subsequence with a function (item) of True. Will try to return and sequence the same type). Sequence returns the same type when it is a string or tuple, and other cases returns a list. Example: Returns an integer that cannot be divisible by 2 and 3:

>>> def f (x): return x% 2! = 0 and x% 3! = 0...>>> Filter (f, Range (2, 25)) [5, 7, 11, 13, 17, 19, 23]

Map (function, sequence) invokes function (item) for each element and returns the return value as a list. For example, the calculation cube:

>>> def Cube (x): Return x*x*x...>>> Map (cube, range (1, 11)) [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

PyautoguiResources
  • Author Blog: http://my.oschina.net/u/1433482

  • contact Xu Rongzhong python development Automation test group 113938272 Weibo Http://weibo.com/cizhenshi.

  • Python 2.7 Official English tutorial: https://docs.python.org/2/tutorial/


Python 2.7 Chinese tutorial and automated test Introduction (3)--to be sorted

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.