Need help with List of Lists - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Need help with List of Lists (/thread-9647.html) Pages:
1
2
|
Need help with List of Lists - johnissa - Apr-21-2018 Hi i really need help with creating a list of lists using a for loop under the following conditions: a = [-1, 2, -2, 3, 41, 38, 22, 10, -1, 3] b = [40, 30, 20, 10] list0 = a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3] = 10 list1 = a[1]*b[0] + a[2]*b[1] + a[3]*b[2] + a[4]*b[3] = 490 list2 = a[2]*b[0] + a[3]*b[1] + a[4]*b[2] + a[5]*b[3] = 1210 and so on.... the answer is the final list of values below answer = [10, 490, 1210, 2330, 3320, 2370, 1190] RE: Need help with List of Lists - Larz60+ - Apr-21-2018 what have you tried so far? here's a hint: for each item in b list, iterate over a list index from 0 to len (a list) - len (b list) RE: Need help with List of Lists - johnissa - Apr-21-2018 i have tried the following: a = [-1, 2, -2, 3, 41, 38, 22, 10, -1, 3] b = [40, 30, 20, 10] k=0 for i in range(0,len(a)): k = k +1 ab = a[k:(k+4)] if len(ab) != 4: break else: print(ab)This gives me lists of lists i need from a, but now i am stuck on how to actually multiple it by the values in b RE: Need help with List of Lists - Larz60+ - Apr-21-2018 If you code your loops like below, you won't need a break list_a = [-1, 2, -2, 3, 41, 38, 22, 10, -1, 3] list_b = [40, 30, 20, 10] e_len = len(list_a) - len(list_b) + 1 for a_idx in range(e_len): elist = [] for b_idx, bval in enumerate(list_b): elist.append(list_a[a_idx + b_idx]*bval) print(elist)you can use sum on a list like sum(listname) and print directly do you know how to use the format statement yet? RE: Need help with List of Lists - johnissa - Apr-21-2018 We have not used the enumerate function or the format statement, is there a way to code it without using these? RE: Need help with List of Lists - Larz60+ - Apr-21-2018 Sure,just use a separate index: list_a = [-1, 2, -2, 3, 41, 38, 22, 10, -1, 3] list_b = [40, 30, 20, 10] e_len = len(list_a) - len(list_b) + 1 for a_idx in range(e_len): elist = [] b_idx = 0 for bval in list_b: elist.append(list_a[a_idx + b_idx]*bval) b_idx += 1 print(elist) RE: Need help with List of Lists - johnissa - Apr-21-2018 one more question, when i sum the elist: print(sum(elist)) i get all the final values, how would i put all those final values in a python list so that the answer comes out like the following: answer = [10, 490, 1210, 2330, 3320, 2370, 1190] rather than 10 490 1210 and so onthanks for all the help. RE: Need help with List of Lists - Larz60+ - Apr-21-2018 think about that. append the value to an empty list. before the loop, create an empty list: answers = []after you create the summation, append to answers: answers.append(thesum) RE: Need help with List of Lists - pyghorz - Apr-22-2018 The answer to the code below is a = [-1, 2, -2, 3, 41, 38, 22, 10, -1, 3] b = [40, 30, 20, 10] answer = [] #loop through a and compute for i in range(len(a)): if i==7: break answer.append(a[i]*b[0]+a[i+1]*b[1]+a[i+2]*b[2]+a[i+3]*b[3]) #get the list print(answer) A better revised code is: a = [-1, 2, -2, 3, 41, 38, 22, 10, -1, 3] b = [40, 30, 20, 10] answer = [] #output the list print('a =',a,'\nb =',b,'\n') #loop through a and compute for i in range(len(a)): if i==7: break value = (a[i]*b[0]+a[i+1]*b[1]+a[i+2]*b[2]+a[i+3]*b[3]) answer.append(value) print('list'+str(i),'=', a[i],'*',b[0],'+',a[i+1],'*',b[1],'+',a[i+2],'*',b[2],'+',a[i+3],'*',b[3],'=',value) #get the list print('\nAnswer = ' + str(answer))The output is: a = [-1, 2, -2, 3, 41, 38, 22, 10, -1, 3] b = [40, 30, 20, 10] list0 = -1 * 40 + 2 * 30 + -2 * 20 + 3 * 10 = 10 list1 = 2 * 40 + -2 * 30 + 3 * 20 + 41 * 10 = 490 list2 = -2 * 40 + 3 * 30 + 41 * 20 + 38 * 10 = 1210 list3 = 3 * 40 + 41 * 30 + 38 * 20 + 22 * 10 = 2330 list4 = 41 * 40 + 38 * 30 + 22 * 20 + 10 * 10 = 3320 list5 = 38 * 40 + 22 * 30 + 10 * 20 + -1 * 10 = 2370 list6 = 22 * 40 + 10 * 30 + -1 * 20 + 3 * 10 = 1190 Answer = [10, 490, 1210, 2330, 3320, 2370, 1190]Hope you find the answer useful. PyGhorz from Nigeria RE: Need help with List of Lists - Larz60+ - Apr-22-2018 here's another approach: list_a = [-1, 2, -2, 3, 41, 38, 22, 10, -1, 3] list_b = [40, 30, 20, 10] e_len = len(list_a) - len(list_b) + 1 for a_idx in range(e_len): elist = [] b_idx = 0 for bval in list_b: if b_idx == 0: print('list{} = '.format(a_idx), end='') else: print(' + ', end='') print('a[{}]*b[{}]'.format(a_idx, b_idx), end='') elist.append(list_a[a_idx + b_idx]*bval) b_idx += 1 print(' = {}'.format(sum(elist)))which results in:
|