数据结构:List常用操作
2018-01-11 20:11
253 查看
Reference:Python官方Document Tutorial
https://docs.python.org/3/reference/datamodel.html#object.__contains__
This chapter describes some things you’ve learned about already in more detail, and adds some new things as well.
In general:
5.1.0list常用方法,
4000
Add an item to the end of the list. Equivalent to
Extend the list by appending all the items from the iterable. Equivalent to
Insert an item at a given position. The first argument is the index of the element before which to insert, so
at the front of the list, and
equivalent to
Remove the first item from the list whose value is x. It is an error if there is no such item.
Remove the item at the given position in the list, and return it. If no index is specified,
and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python
Library Reference.)
Remove all items from the list. Equivalent to
Return zero-based index in the list of the first item whose value is x. Raises a
there is no such item.
The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to
the beginning of the full sequence rather than the start argument.
Return the number of times x appears in the list.
Sort the items of the list in place (the arguments can be used for sort customization, see
their explanation).
Reverse the elements of the list in place.
Return a shallow copy of the list. Equivalent to
An example that uses most of the list methods:
>>>
You might have noticed that methods like
only modify the list have no return value printed – they return the default
is a design principle for all mutable data structures in Python.
The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”). To add an item to the top of the stack, use
To retrieve an item from the top of the stack, use
an explicit index. For example:
>>>
It is also possible to use a list as a queue, where the first element added is the first element retrieved (“first-in, first-out”); however, lists are not efficient for this purpose. While appends and pops from the end of list are fast, doing inserts or pops
from the beginning of a list is slow (because all of the other elements have to be shifted by one).
To implement a queue, use
was designed to have fast appends and pops from both ends. For example:
>>>
List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that
satisfy a certain condition.
For example, assume we want to create a list of squares, like:
>>>
Note that this creates (or overwrites) a variable named
after the loop completes. We can calculate the list of squares without any side effects using:
or, equivalently:
which is more concise and readable.
A list comprehension consists of brackets containing an expression followed by a
then zero or more
The result will be a new list resulting from evaluating the expression in the context of the
which follow it. For example, this listcomp combines the elements of two lists if they are not equal:
>>>
# 语法 [expression,for 判断语句 (if 判断语句)]
其中expression为每个迭代对象的返回值, if(如果有)在for后面
and it’s equivalent to:
>>>
Note how the order of the
is the same in both these snippets.
If the expression is a tuple (e.g. the
the previous example), it must be parenthesized.
>>>
List comprehensions can contain complex expressions and nested functions:
>>>
The initial expression in a list comprehension can be any arbitrary expression, including another list comprehension.
Consider the following example of a 3x4 matrix implemented as a list of 3 lists of length 4:
>>>
The following list comprehension will transpose rows and columns:
>>>
As we saw in the previous section, the nested listcomp is evaluated in the context of the
follows it, so this example is equivalent to:
# 不用列表解析,而是用for循环代替实现:
>>>
which, in turn, is the same as:
>>>
In the real world, you should prefer built-in functions to complex flow statements. The
would do a great job for this use case:
# 特殊:要记住!!
>>>
See Unpacking Argument Lists for details on the asterisk in this line.
https://docs.python.org/3/reference/datamodel.html#object.__contains__
This chapter describes some things you’ve learned about already in more detail, and adds some new things as well.
In general:
5.1.0list常用方法,
5.1.1. Using Lists as Stacks
5.1.2. Using Lists as Queues
5.1.3. List Comprehensions
5.1.4. Nested List Comprehensions
4000
The list data type has some more methods. Here are all of the methods of list objects:
5.1.0 List常用方法
list.
append(x)
Add an item to the end of the list. Equivalent to
a[len(a):] = [x].
list.
extend(iterable)
Extend the list by appending all the items from the iterable. Equivalent to
a[len(a):] = iterable.
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 an 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 is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python
Library Reference.)
list.
clear()
Remove all items from the list. Equivalent to
del a[:].
list.
index(x[, start[, end]])
Return zero-based index in the list of the first item whose value is x. Raises a
ValueErrorif
there is no such item.
The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to
the beginning of the full sequence rather than the start argument.
list.
count(x)
Return the number of times x appears in the list.
list.
sort(key=None, reverse=False)
Sort the items of the list in place (the arguments can be used for sort customization, see
sorted()for
their explanation).
list.
reverse()
Reverse the elements of the list in place.
list.
copy()
Return a shallow copy of the list. Equivalent to
a[:].
An example that uses most of the list methods:
>>>
>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana'] >>> fruits.count('apple') 2 >>> fruits.count('tangerine') 0 >>> fruits.index('banana') 3 >>> fruits.index('banana', 4) # Find next banana starting a position 4 6 >>> fruits.reverse() >>> fruits ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange'] >>> fruits.append('grape') >>> fruits ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape'] >>> fruits.sort() >>> fruits ['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear'] >>> fruits.pop() 'pear'
You might have noticed that methods like
insert,
removeor
sortthat
only modify the list have no return value printed – they return the default
None. [1]This
is a design principle for all mutable data structures in Python.
5.1.1. Using Lists as Stacks
The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”). To add an item to the top of the stack, use append().
To retrieve an item from the top of the stack, use
pop()without
an explicit index. 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]
5.1.2. Using Lists as Queues
It is also possible to use a list as a queue, where the first element added is the first element retrieved (“first-in, first-out”); however, lists are not efficient for this purpose. While appends and pops from the end of list are fast, doing inserts or popsfrom the beginning of a list is slow (because all of the other elements have to be shifted by one).
To implement a queue, use
collections.dequewhich
was designed to have fast appends and pops from both ends. For example:
>>>
>>> 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 arrival deque(['Michael', 'Terry', 'Graham'])
5.1.3. List Comprehensions
List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements thatsatisfy a certain condition.
For example, assume we want to create a list of squares, like:
>>>
>>> squares = [] >>> for x in range(10): ... squares.append(x**2) ... >>> squares [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Note that this creates (or overwrites) a variable named
xthat still exists
after the loop completes. We can calculate the list of squares without any side effects using:
squares = list(map(lambda x: x**2, range(10)))
or, equivalently:
squares = [x**2 for x in range(10)]
which is more concise and readable.
A list comprehension consists of brackets containing an expression followed by a
forclause,
then zero or more
foror
ifclauses.
The result will be a new list resulting from evaluating the expression in the context of the
forand
ifclauses
which follow it. For example, this listcomp combines the elements of two lists if they are not equal:
>>>
>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y] [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
# 语法 [expression,for 判断语句 (if 判断语句)]
其中expression为每个迭代对象的返回值, if(如果有)在for后面
and it’s equivalent to:
>>>
>>> combs = [] >>> for x in [1,2,3]: ... for y in [3,1,4]: ... if x != y: ... combs.append((x, y)) ... >>> combs [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
Note how the order of the
forand
ifstatements
is the same in both these snippets.
If the expression is a tuple (e.g. the
(x, y)in
the previous example), it must be parenthesized.
>>>
>>> vec = [-4, -2, 0, 2, 4] >>> # create a new list with the values doubled >>> [x*2 for x in vec] [-8, -4, 0, 4, 8] >>> # filter the list to exclude negative numbers >>> [x for x in vec if x >= 0] [0, 2, 4] >>> # apply a function to all the elements >>> [abs(x) for x in vec] [4, 2, 0, 2, 4] >>> # call a method on each element >>> freshfruit = [' banana', ' loganberry ', 'passion fruit '] >>> [weapon.strip() for weapon in freshfruit] ['banana', 'loganberry', 'passion fruit'] >>> # create a list of 2-tuples like (number, square) >>> [(x, x**2) for x in range(6)] [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)] >>> # the tuple must be parenthesized, otherwise an error is raised >>> [x, x**2 for x in range(6)] File "<stdin>", line 1, in <module> [x, x**2 for x in range(6)] ^ SyntaxError: invalid syntax >>> # flatten a list using a listcomp with two 'for' >>> vec = [[1,2,3], [4,5,6], [7,8,9]] >>> [num for elem in vec for num in elem] [1, 2, 3, 4, 5, 6, 7, 8, 9]
List comprehensions can contain complex expressions and nested functions:
>>>
>>> from math import pi >>> [str(round(pi, i)) for i in range(1, 6)] ['3.1', '3.14', '3.142', '3.1416', '3.14159']
5.1.4. Nested List Comprehensions
The initial expression in a list comprehension can be any arbitrary expression, including another list comprehension.Consider the following example of a 3x4 matrix implemented as a list of 3 lists of length 4:
>>>
>>> matrix = [ ... [1, 2, 3, 4], ... [5, 6, 7, 8], ... [9, 10, 11, 12], ... ]
The following list comprehension will transpose rows and columns:
>>>
>>> [[row[i] for row in matrix] for i in range(4)] [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
As we saw in the previous section, the nested listcomp is evaluated in the context of the
forthat
follows it, so this example is equivalent to:
# 不用列表解析,而是用for循环代替实现:
>>>
>>> transposed = [] >>> for i in range(4): ... transposed.append([row[i] for row in matrix]) ... >>> transposed [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
which, in turn, is the same as:
>>>
>>> transposed = [] >>> for i in range(4): ... # the following 3 lines implement the nested listcomp ... transposed_row = [] ... for row in matrix: ... transposed_row.append(row[i]) ... transposed.append(transposed_row) ... >>> transposed [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
In the real world, you should prefer built-in functions to complex flow statements. The
zip()function
would do a great job for this use case:
# 特殊:要记住!!
>>>
>>> list(zip(*matrix)) [(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]
See Unpacking Argument Lists for details on the asterisk in this line.
相关文章推荐
- List Control中常用的操作函数
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- 常用数据结构————数组操作
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- socket 常用数据结构和操作函数归纳
- TStringList 常用操作 (转自万一老师)
- 常用数据结构2——栈,实现PUSH、POP和取最小值操作算法时间复杂度为o(1)
- linux内核中常用的数据结构和操作详解
- 【数据结构_链表_List_0952】单链表插入实践的操作
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- Python - List常用操作
- 笔试面试常考数据结构-单链表常用操作编程实现
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- 常用数据结构之顺序结构List实现
- python中list常用操作实例详解
- [技术天地] Java 常用数据结构深入分析(Vector、ArrayList、List、Map)
- python list常用操作
- Java数据结构---链表常用操作(I)
- 常用的数据结构操作flash模拟演示