Posts: 9
Threads: 4
Joined: Apr 2018
Apr-21-2018, 01:23 AM
(This post was last modified: Apr-21-2018, 01:23 AM by johnissa.)
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]
Posts: 12,054
Threads: 488
Joined: Sep 2016
Apr-21-2018, 03:50 AM
(This post was last modified: Apr-21-2018, 03:50 AM by Larz60+.)
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)
Posts: 9
Threads: 4
Joined: Apr 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
Posts: 12,054
Threads: 488
Joined: Sep 2016
Apr-21-2018, 05:10 AM
(This post was last modified: Apr-21-2018, 05:10 AM by Larz60+.)
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?
Posts: 9
Threads: 4
Joined: Apr 2018
We have not used the enumerate function or the format statement, is there a way to code it without using these?
Posts: 12,054
Threads: 488
Joined: Sep 2016
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)
Posts: 9
Threads: 4
Joined: Apr 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 on thanks for all the help.
Posts: 12,054
Threads: 488
Joined: Sep 2016
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)
Posts: 3
Threads: 0
Joined: Apr 2018
Apr-22-2018, 04:09 PM
(This post was last modified: Apr-22-2018, 04:21 PM by pyghorz.)
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
Posts: 12,054
Threads: 488
Joined: Sep 2016
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:
Output: list0 = a[0]*b[0] + a[0]*b[1] + a[0]*b[2] + a[0]*b[3] = 10
list1 = a[1]*b[0] + a[1]*b[1] + a[1]*b[2] + a[1]*b[3] = 490
list2 = a[2]*b[0] + a[2]*b[1] + a[2]*b[2] + a[2]*b[3] = 1210
list3 = a[3]*b[0] + a[3]*b[1] + a[3]*b[2] + a[3]*b[3] = 2330
list4 = a[4]*b[0] + a[4]*b[1] + a[4]*b[2] + a[4]*b[3] = 3320
list5 = a[5]*b[0] + a[5]*b[1] + a[5]*b[2] + a[5]*b[3] = 2370
list6 = a[6]*b[0] + a[6]*b[1] + a[6]*b[2] + a[6]*b[3] = 1190
|