# # Scanning and constructing list of lists # # Practice # # Preliminary notes: - Presumed knowledge: - That practiced in pervious videos - Basic structure of strings, lists - for loop, while loop - The functions range(), len(), eval() - The operators in, *, + - Relation to tuples - We will talk about multiple assignmet - Use only the studied material. In particular do not use type functions # The basic problem # Q. 1 # Given this list: # [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] # Write a program that prints all values # in the list, each in a separate line. lst = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] for firstVal, secondVal, thirdVal in lst: print(firstVal) print(secondVal) print(thirdVal) x = 1 y = 2 z = 3 x, y, z = 1, 2, 3 print(x) print(y) print(z) # multiple assignment # Q. 2 # write a program that given a list, # prints all values in each list, including values # in nested lists. Assume that there is only one # nesting level. Examples for lists: lst1 = [ [1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11, 12] ] lst2 = [[1], [2, 2, 2], [5, 6, 7, 8], [9, 10, 11, 12], [7] ] lst3 = [ [], [4, 5, 6, 7], [8, 9, 10, 11, 12] ] lst4 = ['xyz', 7, 5, 2, [], [4, 5, 6, 7], [8, 9, 10, 11, 12] ] lst5 = [] for val in lst5: if type(val) == list: for num in val: print(num) else: print(val) # Q. 3 # Write a program that produces a random positive integer n # in range 1-10, and next produces a list of lists # representing the multiplication table n X n. # E.g. for n = 5 the list is: # [[1, 2, 3, 4, 5], # [2, 4, 6, 8, 10], # [3, 6, 9, 12, 15], # [4, 8, 12, 16, 20], # [5, 10, 15, 20, 25]] # import random # n = random.randint(5, 10) n = 5 table = [] for i in range(1, n + 1): table = table + [list(range(1 * i, (n + 1) * i, i))] print(table) # Q. 4 # Write a program that iteratively produces # a list of lists. In each iteration, the program # receives any number of integers, produces # an inner list out of them, and adds this inner # list to the list. The iteration stops when the # string stop is entered. proceed = input("Do you want to continue - yes or stop: ") lst = [] while proceed == "yes": num = input("Enter a number or q to stop: ") innerLst = [] while num != 'q': innerLst = innerLst + [int(num)] num = input("Enter a number or q to stop: ") lst = lst + [innerLst] proceed = input("Do you want to continue - yes or stop: ") print(lst) # Q. 5 # write a program that given a list of lists, # prints all values in index 0 (if there are any), # then all values in index 1, then all values # in index 2, etc. # Examples for lists: lst1 = [ [1, 2, 3], [4, 5, 6], [7, 8, 9, 9] ] lst2 = [ [1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11, 12] ] lst3 = [[1], [2, 2, 2], [5, 6, 7, 8], [9, 10, 11, 12], [7] ] lst4 = [ [], [4, 5, 6, 7], [8, 9, 10, 11, 12] ] lst5 = [] ind = 0 atLeastOne = True while atLeastOne: print("ind =", ind) for innerLst in lst4: atLeastOne = False if len(innerLst) > ind: print(innerLst[ind]) atLeastOne = True ind = ind + 1 lst = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] 1 4 7 10 2 5 8 11 3 6 9 12 for i in range(len(lst[0])): for innerLst in lst: print(innerLst[i]) # Q. 6 # Write a program that uses a list representing # a table holding information about incomes: # ID GENDER INCOME # 2121 1 5326 # 3332 1 2136 # 4546 2 7632 # 1217 1 6661 # . . . # The lists is structured by columns: # [ ['ID', 2121, 3332, 4546, 1217], # ['GENDER', 1, 1, 2, 1], # ['INCOMD', 5326, 2136, 7632, 6661]] # Given a list having this structure, # the program receives an ID and prints # the corresponding INCOME. table = [ ['ID', 2121, 3332, 4546, 1217], ['GENDER', 1, 1, 2, 1], ['INCOME', 5326, 2136, 7632, 6661] ] yourID = 3332 for i in range(len(table[0])): if table[0][i] == yourID: print(table[2][i]) print(table[0][1]) Q. 7 # Write a program that uses a list representing # a table holding information about incomes: # ID GENDER INCOME # 2121 1 5326 # 3332 1 2136 # 4546 2 7632 # 1217 1 6661 # . . . # The lists is structured by columns: # [ ['ID', 2121, 3332, 4546, 1217], # ['GENDER', 1, 1, 2, 1], # ['INCOME', 5326, 2136, 7632, 6661]] # Given a list having this structure, # the program produces a list that stores # data by rows: # [ ['ID', 'GENDER', 'INCOME'], # [1, 1, 5326], # [2, 1, 2136], # [3, 2, 7632], # [4, 1, 6661]] # Chllenge: write a program that does the opposite. table1 = [ ['ID', 2121, 3332, 4546, 1217], ['GENDER', 1, 1, 2, 1], ['INCOME', 5326, 2136, 7632, 6661]] table2 = [] for i in range(len(table1[0])): row = [] for innerLst in table1: row = row + [innerLst[i]] table2 = table2 + [row] print(table2)