Common ways to learn Python (3) list by C

Source: Internet
Author: User
Tags shallow copy


View an object's Id:id (unknown Origin object)
Check the data type of a data: type (data or variable)
Query the number of elements of a sequence object: Len (Sequence object)
See which methods are available for a class: Dir (data or variable or class)
To view the usage instructions for a class or function: Help (unknown origin data)


L.append (obj)
Adds a new object at the end of the list.
Entry: Obj-the object to be added to the list
return: None

L.clear ()
Clears the entire list.
Entry: None
return: None

L.copy ()
Copy the list.
Entry: None
Return: New list, List type
Note: The copy is a shallow copy, and the new list and the original list have different IDs, but the ID of the element is the same, and if you add elements to the new list, the original list will not be affected, but if you manipulate the element object itself, the two lists will be affected.

L.count (obj)
Counts the number of occurrences of an element in a list.
Entry: Obj-the object to be added to the list
Return: Number of times, type int

L.extend (obj)
Expands the original list with an iterative obj, adding all the elements in the iteration object to the list in turn.
Entry: obj-iterative Object
return: None

L.index (obj)
Find the index position of the first occurrence of a value from the list.
Enter parameter: obj-unknown origin element
Return: index position, int type

L.insert (Index,obj)
Inserts an element at the specified position where the element and its subsequent elements all move backwards one position.
Enter parameter: index-index position, int type
Obj-element to be inserted
return: None

L.pop (Index)
Returns the element at the specified position in the list, and removes the element from the list
Entry: index-index position, int type, default is-1
Return: element of index position

L.remove (obj)
Removes the first occurrence of a specified element from the list.
Enter parameter: obj-the element to remove, note that it is not an index
return: None

L.reverse ()
Reverses the arrangement of elements in the list.
Entry: None
return: None

L.sort (key, reverse)
Sorts the original list. The elements in the list are of the same type
Entry: Key-sort function, default none, use default small-to-large sort
Reverse-whether reverse (from large to small), bool type, default value False
return: None

Description of the sort function:
Entry: The elements in the list, filled in, followed by only one element at a time, so the function must be allowed to only fill in one parameter
Return: Sort by the elements of a number, string equivalent type, sort arranges the newly created elements again, arranges the original elements of their mapping, and if multiple values are returned, the first return value is the highest priority, the second is the second priority, and so on
Example:

Li = [[[1,2,3],[2,3,2],[4,5,6],[1,3,2],[2,3,3],[2,4,1]]def  KK (x):return x[2  ]li.sort (Key=kk)print(LI)
[[2, 4, 1], [2, 3, 2], [1, 3, 2], [1, 2, 3], [2, 3, 3], [4, 5, 6]]

  

Arrange with the third element of each element in the list

def JJ (x): return x[0],x[2]li.sort (Key=JJ)print(LI)
[[1, 3, 2], [1, 2, 3], [2, 4, 1], [2, 3, 2], [2, 3, 3], [4, 5, 6]]

  

Arranges the first element of each element in the list, and then arranges two times with the third element if the element value is the same

Common ways to learn Python (3) list by C

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.