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
#12
I come from a 'C' and 'C++' background, many years, now I'm hooked on Python.
Reply
#13
Great job Larz60+. In post #10 Larz60+ output was:
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
I believe the output should be (changed the a indices in output - calculations are identical):
Output:
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 list3 = a[3]*b[0] + a[4]*b[1] + a[5]*b[2] + a[6]*b[3] = 2330 list4 = a[4]*b[0] + a[5]*b[1] + a[6]*b[2] + a[7]*b[3] = 3320 list5 = a[5]*b[0] + a[6]*b[1] + a[7]*b[2] + a[8]*b[3] = 2370 list6 = a[6]*b[0] + a[7]*b[1] + a[8]*b[2] + a[9]*b[3] = 1190
Small change in the source code - .format(a_idx [color=#E74C3C]+ b_idx[/color][b][/b], b_idx) was .format(a_idx, b_idx)
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, b_idx), end='')
        elist.append(list_a[a_idx + b_idx]*bval)
        b_idx += 1
    print(' = {}'.format(sum(elist)))
Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#14
yes, missed that
Reply


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