A two-dimensional list puts other lists into one list as elements of the list, which is a nesting of lists . Those who have been exposed to C or C++ may understand the concept of arrays. In Python, arrays exist in third-party libraries. Therefore, if we want to use array methods in Python without installing third-party plug-ins, we must use a two-dimensional list. This method.
In fact, the elements in the list can be of any data type. Let's look at an example:
my_list=[[1,2,3,4,5],'dotcpp',{1,2,3,},('www','dotcpp','com')]print(my_list)
The output is:
[[1,2,3,4,5],'dotcpp',{1,2,3},('www','dotcpp','com')]
This list contains lists, strings, sets, and tuples. When all the elements in a list are lists, it is our most commonly used two-dimensional list. The following will introduce several methods of creating and accessing two-dimensional lists.
The direct creation method is to enter the list name directly after defining it. Let's define a simple small keyboard, namely 1-9:
my_list=[[1,2,3],[4,5,6],[7,8,9]]print(my_list)
The output is:
[[1,2,3],[4,5,6],[7,8,9]]
When we understand it, we can understand it like this:
As can be seen from the figure, my_list[0] corresponds to [1,2,3], so when we access 1, its corresponding coordinates are (0,0), and we can access it through the index, that is, my_list[0 ][0], its value corresponds to 1, and other values are accessed in the same way. For example, the access method corresponding to element 9 is my_list[2][2].
The creation of a list can also be achieved using a loop, usually using a for loop. Let's create a 6*6 two-dimensional list. The code is as follows:
my_list=[]#First create a main list foriinrange(6):#Loop through 6 times and create a new list for each element of the main list in turn j=[]#First create a new list and add it to the main list my_list. append(j)forminrange(1,7):j.append(m)#Add elements to the sublist print(my_list)#Output
The output is:
[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3, 4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]]
The actual output result should be in one line. It is arranged like this for the convenience of everyone's understanding. You can also try to create a two-dimensional list through a while loop.
Two-dimensional lists can also be created using list comprehensions. We just learned list comprehensions in the previous section, so we can use this method directly to simplify our code.
We once again create a two-dimensional list with six rows and six columns. Its code is relatively simple. The code is as follows:
my_list=[[iforiinrange(1,7)]forjinrange(1,7)]print(my_list)
The output is:
[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3, 4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]]
This method not only saves time but also optimizes the code. It is equivalent to creating 6 lists in a loop and assigning 6 values. Finally, they are placed in a list to form a two-dimensional list.
Let’s learn more through an example.
We want to create a 6*6 list, and then the values in it are randomly selected from 1-100. The code is as follows:
importrandom#random is a module in the system library and can be called at any time. Here is a brief introduction my_list=[]#First create the list foriinrange(6):#Create 6 lists in a loop and put them into my_list j=[]my_list.append (j)forkinrange(6):c=random.randint(1,100)#Call a method in random to generate random numbers, and assign the value of the generated random number to cj.append(c)#Add the value of c print(my_list) in list j
The output is:
[[52,83,15,35,54,60],[17,61,77,99,60,50],[88,81,9,61,76,95],[21,52,20, 49,10,61],[94,81,48,27,80,9],[1,94,57,66,95,97]]
The idea of this question is consistent with the second method above. Students who are not proficient in list derivation can use this method in the early stage. Compared with the previous example, it adds a random value function.
Regarding the two-dimensional list, this is very important for students who want to participate in the competition. Whether you encounter the maze problem or the 2n queen problem later, you cannot do without the use of the two-dimensional list. Therefore, I will briefly introduce it here. You can As a preliminary understanding, there will be explanations about these two types of questions in the subsequent competition questions. Of course, we can also go further to the two-dimensional list.