Python Forum
Need help with List of Lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with List of Lists
#1
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]
Reply
#2
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)
Reply
#3
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
Reply
#4
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?
Reply
#5
We have not used the enumerate function or the format statement, is there a way to code it without using these?
Reply
#6
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)
Reply
#7
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.
Reply
#8
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)
Reply
#9
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
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Check if a list exists in given list of lists Daniel94 2 2,236 Apr-07-2020, 04:54 PM
Last Post: deanhystad
  List of lists manipulation Stahlios 5 4,120 Apr-18-2018, 07:43 PM
Last Post: Stahlios
  List of lists MarkLogan 3 75,112 Feb-28-2018, 11:21 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020