Python. Nested lists (2D arrays)
 code { font-family: Consolas,"courier new"; background: #f4f4f4; border: 1px solid #ddd; border-left: 3px solid #f36d33; color: #666; page-break-inside: avoid; font-size: 15px; line-height: 1.6; margin-bottom: 1.6em; max-width: 100%; overflow: auto; padding: 1em 1.5em; display: block; word-wrap: break-word; } 11.2.3.4 create nested lists 11.2.3.5 enter elements of nested lists from the keyboard Python. Nested lists (2D arrays) A nested list is a list that is an element of another list. Lists can be nested at any level. We will consider only two-dimensional (2D) lists. The 2D list is a data structure that can be represented in a square table.  Each element of the list is also represented by a list (nested list). To refer to an element in a 2D list, you should at first determine the index of the list element (row) and then determine the index of the nested list element (column). For example, to get the value 8 from a table, you need to write the name of the list lst2D, then specify the index of the list element lst2D[1], then specify the index of the element in the nested list lst2D[1][2]. ```lst2D[1][2] = 8 lst2D[2][0] = 3``` Question. Determine the result of a code snippet: print(lst2D[0][3], lst2D[2][2], lst2D[-1][-3]) Answer. 10 9 6 How to create 2D lists filled with zeros? `lst2D = [[0] * 10 for i in range(5)] ` ```Output: [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]``` How to fill a 2D list with dimensions n*m from the keyboard? If we want to input each value of an element in a new line: ```lst2D = [] # create empty list for i in range(3):     row = [] # create empty list for new row in 2D list     for j in range(4):         row.append(input()) # each element is entered separately     lst2D.append(row) # add new item (list) to 2D list``` If we want to input each row of elements in line: ```lst2D = [] for i in range(3):     lst2D.append(input().split()) # input values of row in a space``` If we want to input each row of integers on one line: ```lst2D = [] for i in range(3):     lst2D.append([int(x) for x in input().split()]) # convert each values to integer``` How to process each element of a matrix? Iterating over indices. We can get and change the value for each element ```for i in range(n): # n rows     for j in range(m): # m columns         lst2D[i][j] = ... # change value of item with i-row and j-column``` Iterating over elements. We can get value for each element ```for row in lst2D: # iterate each element in list     for col in row: # iterate each element in nested list         print(col)``` How to display all elements in a table? ```for i in range(n):  # n rows     for j in range(m): # m columns         print(lst2D[i][j], end='\t') # output in row in a tab     print()``` or ```for row in lst2D: # iterate each element in list     for col in row: # iterate each element in nested list         print(f"{col:5}", end='') # using f-string for alignment     print()``` Questions: 1. Explain what means 2D list/array. 2. Describe the main idea to process elements of the 2D list. Exercises: Ex. 1 Determine the result of code snippets Ex. 2 Fill the blanks Tasks: .u-star-rating-13 { list-style:none; margin:0px; padding:0px; width:65px; height:13px; position:relative; background: url('/.s/t/2001/rating13.png') top left repeat-x } .u-star-rating-13 li{ padding:0px; margin:0px; float:left } .u-star-rating-13 li a { display:block;width:13px;height: 13px;line-height:13px;text-decoration:none;text-indent:-9000px;z-index:20;position:absolute;padding: 0px;overflow:hidden } .u-star-rating-13 li a:hover { background: url('/.s/t/2001/rating13.png') left center;z-index:2;left:0px;border:none } .u-star-rating-13 a.u-one-star { left:0px } .u-star-rating-13 a.u-one-star:hover { width:13px } .u-star-rating-13 a.u-two-stars { left:13px } .u-star-rating-13 a.u-two-stars:hover { width:26px } .u-star-rating-13 a.u-three-stars { left:26px } .u-star-rating-13 a.u-three-stars:hover { width:39px } .u-star-rating-13 a.u-four-stars { left:39px } .u-star-rating-13 a.u-four-stars:hover { width:52px } .u-star-rating-13 a.u-five-stars { left:52px } .u-star-rating-13 a.u-five-stars:hover { width:65px } .u-star-rating-13 li.u-current-rating { top:0 !important; left:0 !important;margin:0 !important;padding:0 !important;outline:none;background: url('/.s/t/2001/rating13.png') left bottom;position: absolute;height:13px !important;line-height:13px !important;display:block;text-indent:-9000px;z-index:1 } Категория: Programming languages | Добавил: bzfar77 (17.01.2022) Просмотров: 644 | Теги: Index, Row, column, Python, nested lists | Рейтинг: 5.0/2
 Всего комментариев: 0