Python Learning (v)--list operation full dialysis

Source: Internet
Author: User

Lists are implemented in the form of classes.

The Create list is actually an instantiation of a class.

Therefore, the list can be manipulated in several ways.

Functions and methods for Python list operations
  

The list operations include the following functions:

1, CMP (List1, LIST2): More than two elements of a list
2. Len (list): Number of elements
3. Max (list): Returns the maximum value of the element
4, Min (list): Returns the minimum value of the list element
5. List (seq): Convert tuples to lists
  
The list operation includes the following methods:

1. List.append (obj): Add a new object at the end of the list
2. List.count (obj): Count the number of occurrences of an element in a list
3. List.extend (seq): Append one time at the end of the list and multiple values in a sequence (extend the original list with a new list)
4, List.index (obj): Find the index position of the first occurrence of a value from the list
5. List.insert (index, obj): inserting objects into the list
6, List.pop (Obj=list[-1]): Removes an element from the list (the last element by default). and returns the value of the element
7, List.remove (obj): Removes the first occurrence of a value in a list
8, List.reverse (): Reverse List of elements
9. List.sort ([func]): Sort the original list
The list operation includes the following methods:


a simple example

>>>   #list. Append (n), append element, receive only one parameter  >>>  a[0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ]>>>  A.append (6 ) >>>  a[ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 6 ]  
>>> #list.count(n) , 计算n在list中出现的次数>>> a[012345676]>>> a.count(6)2
>>>#list. Extend (List1), append List1 to the back of list>>>a[0,1,2,3,4,5,6,7,6]>>>b = [' A ',' B ',' C ',' d ']>>>a[0,1,2,3,4,5,6,7,6]>>>A.extend (b)>>>a[0,1,2,3,4,5,6,7,6,' A ',' B ',' C ',' d ']>>>A.append (b)#能够注意append与extend的差别>>>a[0,1,2,3,4,5,6,7,6,' A ',' B ',' C ',' d ', [' A ',' B ',' C ',' d ']]
>>>#list. Index (n), returns the position of N in the list, if none, throws an exception>>>a[0,1,2,3,4,5,6,7,6]>>>A.index (' x ') Traceback (most recent): File"<pyshell#67>", line1,inch<module> A.index (' x ') ValueError:' x '  is  not inchList>>>A.index (4)4
>>>#list. Insert (Index,var), when the index is inserted into Var, the remaining elements are pushed backwards. Assuming that index is greater than the list length, it will be added later. Assuming that index is less than 0, it is necessary to join at the very beginning>>>a[0,1,2,3,4,5,6,7,6,' A ',' B ',' C ',' d ', [' A ',' B ',' C ',' d ']]>>>A.insert (0,1)>>>a[1,0,1,2,3,4,5,6,7,6,' A ',' B ',' C ',' d ', [' A ',' B ',' C ',' d ']]>>>A.insert ( -, -)>>>a[1,0,1,2,3,4,5,6,7,6,' A ',' B ',' C ',' d ', [' A ',' B ',' C ',' d '], -]>>>
>>> #list.pop() , 返回最后一个元素,而且删除最后一个元素。

List.pop (index), returns the element at index, and deletes the element.>>>a[1,0,1,2,3,4,5,6,7,6,' A ',' B ',' C ',' d ', -]>>>A.pop () ->>>a[1,0,1,2,3,4,5,6,7,6,' A ',' B ',' C ',' d ',]>>>A.pop (0)1>>>a[0,1,2,3,4,5,6,7,6,' A ',' B ',' C ',' d ']

>>>#list. Remove (Var), find Var and delete it. If none, throws an exception>>>a[0,1,2,3,4,5,6,7,6,' A ',' B ',' C ',' d ']>>>A.remove (9) Traceback (most recent): File"<pyshell#98>", line1,inch<module> A.remove (9) ValueError:list.remove (x): X not inchList>>>A.remove (6)>>>a[0,1,2,3,4,5,7,6,' A ',' B ',' C ',' d ']
>>>#list. Reverse (), reverse list>>>a[0,1,2,3,4,5,7,6,' A ',' B ',' C ',' d ']>>>A.reverse ()>>>a[' d ',' C ',' B ',' A ',6,7,5,4,3,2,1,0]
>>>#list. Sort (), sorts the list, and the elements in a are of different types. And look at it for yourself, but I don't usually do it.>>>a[' d ',' C ',' B ',' A ',6,7,5,4,3,2,1,0]>>>A.sort () Traceback (most recent): File"<pyshell#107>", line1,inch<module> a.sort () typeerror:unorderable types:int () < STR ()>>>A = [1,3,2,4,5,6,3,2,1]>>>a[1,3,2,4,5,6,3,2,1]>>>A.sort ()>>>a[1,1,2,2,3,3,4,5,6]

Python Learning (v)--list operation full dialysis

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.