Home  >  Article  >  Backend Development  >  Specific analysis of list() lists in Python

Specific analysis of list() lists in Python

黄舟
黄舟Original
2017-07-24 13:59:041816browse

List is the most flexible ordered collection object type in Python. Different from strings, lists can contain any kind of objects: numbers, strings, custom objects and even other lists. Column tables are mutable objects that support modification in place and can be modified by specifying offsets. Implementation of value transfer and sharding, list method calls, delete statements and other methods.

Commonly used methods in the list:

1.append(x) : Add an element to the end of the list.

>>> list=[1,2,3,4,5,6]
>>> print list
[1, 2, 3, 4, 5, 6]
>>> list.append(7)
>>> list.append(8)
>>> print list
[1, 2, 3, 4, 5, 6, 7, 8]
>>>

2.extend(L) : Expands the list by adding all elements of the specified list.

>>> list
[1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12]
>>> L=[100,200,300,400]
>>> list.extend(L)
>>> print list
[1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300, 400]
>>>

3.insert(i,x) : Insert an element at the specified position. The first parameter is the index of the element to be inserted before it.

>>> print list
[1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300, 400]
>>> list.insert(2,1000)
>>> print list
[1, 2, 1000, 3, 4, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300, 400]
>>>

4.remove(x) : Delete the first element with value x in the linked list. If there is no such element, an error will be returned.

>>>> print list
[1, 2, 1000, 3, 4, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300, 400]
>>> list.remove(1000)
>>> list
[1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300, 400]
>>>

5.pop(i)          : Removes an element from the specified position in the linked list and returns it. If no index is specified, a.pop() returns the last element. The element is then removed from the linked list.

>>>> list
[1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300, 400]
>>> list.pop(3)

4


>>> list
[1, 2, 3, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300, 400]
>>> list.pop()
400
>>> list
[1, 2, 3, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300]
>>>

6.index(x) : Returns the index of the first element in the linked list whose value is x.

>>>> list
[1, 2, 3, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300]
>>> list.index(6)
4
>>>

7.count(x) : Returns the number of times x appears in the linked list.

>>>> list
[1, 2, 3, 3, 3, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300]
>>> list.count(3)
3
>>> list.count(200)
1
>>>

8.sort() : Sort the elements in the linked list appropriately.

9.reverse() : Elements in the inverted linked list.

Use the list as a stack

The linked list method makes it easy to use the linked list as a stack, and the stack serves as specific data Structure, the first element entered is the last to be released (last in, first out). Use append() Method can

add an element to the top of the stack. Use pop() without specifying an index Method can release an element from the top of the stack.

Use the list as a queue

You can also use the linked list as a queue. The queue is a specific data structure. The element that enters first is released first (first in, first in). out). Use the append() method to add elements to the end of the queue, and call pop() with 0

as the parameter. Method can release the first entered element.

Delete elements from a list: Use del to delete elements in segments.

>>> list
[1, 2, 3, 3, 3, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300]
>>> del list[2:4]
>>> list
[1, 2, 3, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300]
>>> del list[0]
>>> list
[2, 3, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300]
>>> del list[5:]
>>> list
[2, 3, 5, 6, 7]
>>> del list[0:]
>>> list
[]
>>> list.append(1)
>>> list.append(2)
>>> list.append(3)
>>> list
[1, 2, 3]
>>>

The above is the detailed content of Specific analysis of list() lists in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn