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
#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


Messages In This Thread
Need help with List of Lists - by johnissa - Apr-21-2018, 01:23 AM
RE: Need help with List of Lists - by Larz60+ - Apr-21-2018, 03:50 AM
RE: Need help with List of Lists - by johnissa - Apr-21-2018, 04:11 AM
RE: Need help with List of Lists - by Larz60+ - Apr-21-2018, 05:10 AM
RE: Need help with List of Lists - by johnissa - Apr-21-2018, 05:35 AM
RE: Need help with List of Lists - by Larz60+ - Apr-21-2018, 05:45 AM
RE: Need help with List of Lists - by johnissa - Apr-21-2018, 07:24 AM
RE: Need help with List of Lists - by Larz60+ - Apr-21-2018, 09:47 AM
RE: Need help with List of Lists - by pyghorz - Apr-22-2018, 04:09 PM
RE: Need help with List of Lists - by Larz60+ - Apr-22-2018, 04:31 PM
RE: Need help with List of Lists - by pyghorz - Apr-22-2018, 04:38 PM
RE: Need help with List of Lists - by Larz60+ - Apr-22-2018, 06:11 PM
RE: Need help with List of Lists - by ljmetzger - Apr-22-2018, 06:24 PM
RE: Need help with List of Lists - by Larz60+ - Apr-22-2018, 09:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Check if a list exists in given list of lists Daniel94 2 2,295 Apr-07-2020, 04:54 PM
Last Post: deanhystad
  List of lists manipulation Stahlios 5 4,217 Apr-18-2018, 07:43 PM
Last Post: Stahlios
  List of lists MarkLogan 3 84,482 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