List
The list List is essentially a linked list. From the implementation point of view of the linked list, each node of the linked list stores a value and a pointer to the next node.
Therefore, the storage of the linked list in memory can be discontinuous, and it is an efficient data structure. So the difference between lists and strings is that lists can be arbitrary
Modifications. Also, the elements of the list are of any data type. Of course, it supports multiple operations such as looping, slicing, etc. If we slice the list,
That result is still a list.
When a list converts a sequence, it is internally looped, and numbers cannot be used as objects of loops, so numbers cannot be converted
is a list; and when the list is converted to a string, the string function treats the entire list as a string, which makes the string ugly. If you want to
To become beautiful, you can write your own loop to solve it.
1. Convert the character == " to a list
string = "abcdefg" print(list(string))
2. List == " String: use for loop
li = [1,2,3,4,"I","love","you"] for i in li: string = string + str(i) print(string)
3. List == " String: The join function can be used when there are only strings in the list.
li = ["I","love","you"] print("".join(li))
4. Let’s talk about the extend(iterable) function, which can expand the original list. The parameter iterable is an iterable object.
li = [1,2,3,4,"I","love","you"] li.extend([6,"here"]) li.extend( " abc " ) #Please note the difference with append print (li) #Output : [1, 2, 3, 4, 'I', 'love', 'you', 6, 'here', ' a', 'b', 'c']
Note: Compared with append, extend is equivalent to looping inside, and then using the append function to append elements to the list
5. Use lists as stacks
Use the append() method to add an element to the top of the stack. Use the pop() method without specifying an index to release an element from the top of the stack.
li = ["1","2","3","4","5"] li.append( " 6 " ) print (li) #Output : ['1', '2', '3', '4', '5', '6'] del_li = li.pop() #Do not add parameter pop print (del_li) #Get the deleted value: 6 print (li) #Display the deletion result: ['1', '2', '3', '4', '5']
6. Use lists as queues
We can also use lists as queues, and queues are specific data structures where the first elements that enter are released first (first-in, first-out). but,
Lists are not very efficient to use this way. Adding and popping from the end of the list is relatively fast; inserting and popping at the head is slow (because, for an element, it is necessary to move
move all elements in the entire list). To implement a queue, use collections.deque, which is designed for fast insertion and deletion at both ends. For example :
from collections import deque #Import the deque function of the collections module li = [ " Lily " , " Lynn Lee " , " John " ] queue = deque(li) #Use the deque function queue.append( " Alger " ) #Insert data into the queue queue.popleft() #Delete the head print (queue) #Print result : deque(['Lynn Lee', 'John ', 'Alger'])
7. List comprehensions
A list comprehension consists of parentheses that contain an expression followed by a for clause, followed by zero or more for or if clauses.
The result is a list consisting of the result of evaluating the expression according to the context of the for and if clauses that follow it. The general form of its expression is:
[expression for [if clause ]] . The following will illustrate the role of list comprehensions with a simple example.
7.1 Generate the power of 2 x in the list -
- Do not use list comprehensions
li = list(map( lambda x: x**2, range(10 ))) print (li) #The print result is: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
- Use list comprehension
li = [x**2 for x in range(10 )] print (li) #The print result is: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
7.2 Generate coordinates according to the condition that the elements of two lists are different——
- Do not use list comprehensions
li = [] for x in [1,2,3]: for y in [3,4,5]: if x != y: li.append((x, y)) print (li) # output: [(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5)]
- Use list comprehension
li = [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y] print(li) #输出:[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
Obviously, the list comprehension is very concise and enhances the readability of the program
7.3 Specification of list comprehensions -
The general form of a list comprehension is: [expression for [if clause ] ], where the expression should be enclosed in parentheses if it is not a simple statement.
Without parentheses : report an error (syntax error)
[x, x**2 for x in range(6) ] #Error
enclosed in parentheses : normal execution
from math import pi li = [str(round(pi, i)) for i in range(1, 4)] print(li) #输出:['3.1', '3.14', '3.142']
7.4 Application example: switching matrix row and column——
mylist = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ] #The first derivation obtains three rows of data of the original matrix, and then prints li = [ [row[i] for row in mylist] for i in range(4 ) ] print (li) #output [[1 for each row , 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
8. Other commonly used functions are summarized as follows: