Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 38
# Topic 1: Filtering # Assume X = ['i2', 'o4', '23', 'e5', '99', 'e7', 'o9', 'i1', 'e7'] # Please classify strings starting with 'i', 'o', and 'e' into three different list variables called ilist, olist, and elist. The rest will be classified into list variable called otherlist. X_list = ['i2', 'o4', '23', 'e5', '99', 'e7', 'o9', 'i1', 'e7'] i_list = [] o_list = [] e_list = [] for x in X_list: if x[0] == 'i': i_list.append(x) elif x[0] == 'o': o_list.append(x) elif x[0] == 'e': e_list.append(x) print ('i_list = %s' % i_list) print ('o_list = %s' % o_list) print ('e_list = %s' % e_list)
i_list = ['i2', 'i1'] o_list = ['o4', 'o9'] e_list = ['e5', 'e7', 'e7']
# Topic 2: List comprehension # Fibonacci Sequence is defined as F(n) = F(n-1) + F(n-2); F(0) = F(1)=1. # Use list comprehension to generate a list of Fibonacci Sequence containing F(0) to F(20). # In mathematics, I will write it as [fab(n) : n = 0, 1, ..., 20]. # So, if direct convert to mathematics, it should looks like the following. BUt I not sure if this is the # the list comprehension that you want. # def fab(n): if n==0 or n ==1: return 1 else: return fab(n-1) + fab(n-2) fab_list = [fab(n) for n in range(20)] print ('fab_list = %s' % fab_list) # Or ...(which look very weird) f_list = [1, 1] nothing = [f_list.append(f_list[-1] + f_list[-2]) for n in range(20-2)] print f_list
fab_list = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765] [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]
# Topic 3: Function # Write a function to return a list of the common elements between two lists without using set or dict in Python. # These two input lists can have duplicate elements. def find_common(a_list, b_list): common_list = [] for a in a_list: if a in b_list: common_list.append(a) # return common_list # # Test # a_list = [1,2, 3, 5] b_list = [1,4, 2] find_common(a_list, b_list)
[1, 2]
# Topic 4: Parsing # As defined in Assignment #1, store the attributes of each gate description in gates.csv (attached) into a two-dimensional list called gates using the code below to read the file. # f = open('gates.csv', 'r') # for line in f: # // parse each line representing a gate # Results should have removed all the white spaces in the lines and I can get a neat print out like below when I type print gates[2] # ['2', 'not', 'B', '-', '-'] # Hints: you may need a type specific method called str.split() which can be found through google. f = open('gates.csv', 'r') token_list_list = [] for line in f: token_list = line.split(",") token_list = [token.strip() for token in token_list] if token_list[0] == 'name': continue token_list_list.append(token_list) # token_list_list = sorted(token_list_list) gate = token_list_list print (gate[2])
['2', 'not', 'B', '-', '-']
a = "hello " a.strip() s = "4, nor, 1, 0, Q" s_list = s.split(",") # s_list [s.strip() for s in s_list]
'hello' ['4', 'nor', '1', '0', 'Q']