Python Forum
Multiply Lists of Lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiply Lists of Lists
#1
I have a set of lists like this
[0.74, 0.91, 1.22, 0.91, 1.07, 1.07, 1.07, 1.07, 1.07, 0.74, 0.91, 0.91, 1.07, 1.07, 0.91, 1.07]
[1.23, 0.97, 2.09, 0.97, 0.83, 0.83, 0.83, 1.23, 0.83, 0.83, 0.83, 0.83, 0.83, 0.83, 0.83, 0.83]
[1, 1.36, 1.83, 0.95, 1, 1, 0.95, 1.22, 0.95, 1, 0.67, 0.67, 0.95, 0.95, 1, 0.67]
[1.22, 1.07, 1.22, 0.65, 1.67, 0.65, 0.65, 1.32, 1.67, 1.67, 1.32, 0.65, 1.67, 1.67, 1.32, 1.67]
[1.1, 1.1, 1.1, 1.1, 1.1, 0.58, 1.1, 1.1, 1.1, 0.58, 1.1, 0.58, 0.58, 0.58, 1.1, 0.58]
[0.91, 0.91, 0.91, 0.91, 0.91, 0.7, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.7, 0.7, 0.7, 0.7]
[0.72, 0.72, 0.72, 0.91, 0.88, 0.88, 0.88, 0.72, 0.91, 0.91, 0.45, 0.88, 0.91, 0.72, 1.25, 0.72]

Then what i have done is combine in to a list of lists
sumrate += [list1, list2, list3, list4, list5, list6, list7]

Then i can add each item from each list and multiply by 10.
multiplyrate = [10 * sum(i) for i in zip(*sumrate)]

But i am looking for a way to multiply each item rather than sum each item.
Reply
#2
taking two lists of equal length:
>>> list1 = [0.74, 0.91, 1.22, 0.91, 1.07, 1.07, 1.07, 1.07, 1.07, 0.74, 0.91, 0.91, 1.07, 1.07, 0.91, 1.07]
>>> list2 = [1.23, 0.97, 2.09, 0.97, 0.83, 0.83, 0.83, 1.23, 0.83, 0.83, 0.83, 0.83, 0.83, 0.83, 0.83, 0.83]
>>> result = [l1 * l2 for l1, l2 in zip(list1, list2)]
>>> print(result)
[0.9102, 0.8827, 2.5498, 0.8827, 0.8881, 0.8881, 0.8881, 1.3161, 0.8881, 0.6142, 0.7553, 0.7553, 0.8881, 0.8881, 0.7553, 0.8881]
Reply
#3
cheers, i can work with that, thanks
Reply
#4
When working with numbers, I would recommend to use Numpy, especially if you have millions of cells:

import numpy as np
list1 = [0.74, 0.91, 1.22, 0.91, 1.07, 1.07, 1.07, 1.07, 1.07, 0.74, 0.91, 0.91, 1.07, 1.07, 0.91, 1.07]
list2 = [1.23, 0.97, 2.09, 0.97, 0.83, 0.83, 0.83, 1.23, 0.83, 0.83, 0.83, 0.83, 0.83, 0.83, 0.83, 0.83]
list1 = np.asarray(list1)
list2 = np.asarray(list2)
result = list1*list2
print(result)
Output:
[0.9102 0.8827 2.5498 0.8827 0.8881 0.8881 0.8881 1.3161 0.8881 0.6142 0.7553 0.7553 0.8881 0.8881 0.7553 0.8881]
Reply
#5
I probably don't understand what you really want.

Not sure why you are using sum or zip if you just want to multiply each item in a list by a constant.

def multiply_each_element(alist, m):
    newlist = [alist[i] * m for i in range(len(alist))]
    return newlist

multiplied_list = multiply_each_element(l1, 10)
You get some weird values for some numbers. Something to do with how Python handles floats, I read about that somewhere.

You could pass 2 or more lists to a similar function and do some list tricks before multiplying. Probably need to find the shortest list first.
Reply
#6
@Pedroski55 When you write for i in range(len(alist)), it often indicates that you should be writing for item in alist. In your case [item * m for item in alist] works very well.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Turn list of arrays into an array of lists Cola_Reb 6 1,730 Jul-20-2022, 06:55 PM
Last Post: Cola_Reb
  Array lists vs Linked Lists sabe 2 1,775 Nov-28-2020, 12:31 AM
Last Post: perfringo
  Creating new lists with names depending on a changing integer in a while loop l_butler 0 1,579 May-24-2020, 04:05 PM
Last Post: l_butler
  Formula with elements of list - If-condition regarding the lists elements lewielewis 2 2,750 May-08-2020, 01:41 PM
Last Post: nnk
  Compare two lists. Count larger values dervast 2 2,853 Dec-11-2019, 11:39 AM
Last Post: perfringo
  sorting 2D lists by column ToffieFaye 8 104,113 Sep-25-2019, 07:30 AM
Last Post: ToffieFaye
  How to work with large lists without crashing Python? Corate 8 14,409 Mar-14-2018, 09:41 PM
Last Post: wavic
  Argument lists sobrio1 7 4,192 Oct-16-2017, 01:54 AM
Last Post: ichabod801
  Calculate the mean of an array across dimension with lists of different length rakhmadiev 2 4,533 Aug-01-2017, 02:52 AM
Last Post: Larz60+
  Are lists Python's name for "variable array" Luke_Drillbrain 6 4,953 Apr-21-2017, 09:14 AM
Last Post: volcano63

Forum Jump:

User Panel Messages

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