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
#11
(Apr-22-2018, 04:31 PM)Larz60+ Wrote: 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

Smile thumbsup, i'd love to code it out in C, but python only is allowed. Welldone!
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,263 Apr-07-2020, 04:54 PM
Last Post: deanhystad
  List of lists manipulation Stahlios 5 4,164 Apr-18-2018, 07:43 PM
Last Post: Stahlios
  List of lists MarkLogan 3 80,626 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