Python2.7 Self-study note 5--list

Source: Internet
Author: User

First, List of common methods


    • list. Append (x)

      Add an item to the end of the list; Equivalent to A[len (a):] = [x].


  • list. Extend (L)

    Extend the list by appending all the items in the given list; Equivalent to A[len (a):] = L.

  • list. Insert (i, x)

    insert an item at a given position. The first argument is the index of the element before which to insert, SO a.insert (0,  x)  inserts at the front of the list, and  a.insert (Len (a),  x)  is equivalent to a.append (x) .

    • list. remove ( x )

      remove the First item from the list whose value is  x . It is a error if there is no such item

  • list. pop ( [ i ] )

    remove the item at the given Position in the list, and return it. If No index is SPECIFIED, a.pop ()  removes and returns the last item in the list. (The square brackets around the  I  in the method signature denote that the parameter was optional, not th At your should type square brackets at that position. You'll see this notation frequently in the Python Library Reference.)

    • list. Index (x)

      Return the index in the list of the first item whose value is x. It is a error if there is no such item

    • list. Count (x)

      Return the number of times x appears in the list

    • list. sort ( cmp=none ,   key=none ,   reverse=false )

      sort the items of the list in place (the arguments can is used for Sort customization, see sorted ()  for their explanation)

      • list. Reverse ()

        Reverse The elements of the list, in place.

Example:


In [1]: a = [66.25, 333, 333, 1, 1234.5]in [2]: Print A.count (333), A.count (66.25), A.count (' X ') 2 1 0In [3]: A.insert (2,- 1) in [4]: A.append (333) in [5]: aout[5]: [66.25, 333,-1, 333, 1, 1234.5, 333]in [6]: A.index (333) out[6]: 1In [7]: A.remove (333) In [8]: A
OUT[8]: [66.25,-1, 333, 1, 1234.5, 333]in [9]: A.reverse () in [ten]: aout[10]: [333, 1234.5, 1, 333,-1, 66.25]in [all]: A.S ORT () in [aout[12]: [-1, 1, 66.25, 333, 333, 1234.5]in []: A.pop () out[13]: 1234.5In []: aout[14]: [-1, 1, 66.25, 3 33, 333]


Second, list as a stack

in [+]: stack = [3, 4, 5]in [+]: stack.append (6) in []: Stack.append (7) in []: stackout[18]: [3, 4, 5, 6, 7]in []: s Tack.pop () out[19]: 7In []: stackout[20]: [3, 4, 5, 6]in []: Stack.pop () out[21]: 6In []: Stack.pop () out[22]: 5


Third, list as queue


To use the queue efficiently, use the Collection.deque

In [All]: from collections import Dequein [+]: queue = deque (["Eric", "John", "Michael"]) in [+]: Queue.append ("Terry") in [+]: Queue.append ("Graham") in [+]: Queue.popleft () out[27]: ' Eric ' in [+]: Queue.popleft () out[28]: ' John ' in [29]: QUEUEOUT[29]: deque ([' Michael ', ' Terry ', ' Graham '])


Iv. common functions of programming


There are three built-in functions that are useful when used in conjunction with lists: Filter (), map (), reduce ()



The filter (function,sequence) function returns a set of sequences that contain elements that use function functions (sequence) to return a value that is true, and if sequence is a string or tuple, the result is the same type, Otherwise the result type is list:


in [+]: def f (x): return x% 3 = = 0 or x% 5 = = 0In [+]: Filter (F, Range (2)) out[31]: [3, 5, 6, 9, 10, 12, 15, 18, 20 , 21, 24]


Map (function,sequence) invokes function functions (sequence) for each element of the sequence sequence and returns a list of result groups:


in [+]: def cube (x): Return x*x*xin [+]: Map (cube, range (1, one)) out[33]: [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
In [the]: seq = range (8) in [+]: def add (x, y): return x+yin [+]: Map (add, seq, seq) out[36]: [0, 2, 4, 6, 8, 10, 12, 14]


The reduce (function,sequence) function returns a single number as the result of the first number of calls to the function, and the result is a function call to the next element as a value:

In [PNS]: def add (x, y): return x+yin [+]: Reduce (Add, range (1, one)) out[38]: 55

In [MAX]: def sum (seq): ....: def add (x, y): Return x+y ....: Return reduce (add, seq, 0) ....: in [+]: Sum (RA Nge (1, one)) out[40]: 55In [in]: sum ([]) out[41]: 0


V. List parsing (listing comprehension)


The normal way to create a list is:

In [all]: squares = []in]: for x in range: ....: Squares.append (x**2) ....: in []: squaresout[44]: [0 , 1, 4, 9, 16, 25, 36, 49, 64, 81]

You can also create it in the following ways:

In [the]: squares = [x**2 for x in range (10)]

is also equivalent to:

in [+]: Squares = map (lambda x:x**2, Range (10))

in [+]: [(x, Y) for x in [All-in-A-z] for y in [3,1,4] if x! = y]out[47]: [(1, 3), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

Equivalent to:

In [to]: Combs = []in]: For x in []: ....: for y in [3,1,4]: ....: if x! = y: ....: Combs.append ((x, y)) ....: in [[]: Combsout[50]: [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]


This article is from the "Ordinary Road" blog, please be sure to keep this source http://linjohn.blog.51cto.com/1026193/1611586

Python2.7 Self-study note 5--list

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.