Search code examples
pythonlistmultiplication

Function that multiplies each element of one list with four elements of second list in a loop to get a new list


I have two lists,one of length 40 and the other of length 10.I want to multiply first four elements of the 40 list with the first element of the second list and loop that for the entire first list of 40 to a get a new list which is the product of these two. Any suggestions on how to go about this?

from itertools import zip_longest
def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)

v=[[a1/ b1 for a1 in a4] for a4, b1 in zip(grouper(active, 4), passive)]
active=[56.93977737426758,
 54.12062072753906,
 54.89398765563965,
 55.214101791381836,
 54.29464149475098,
 53.80845832824707,
 54.46353721618652,
 54.49761962890625,
 53.01671028137207,
 53.872962951660156,
 53.156455993652344,
 53.20746994018555,
 52.529762268066406,
 56.03120040893555,
 54.122426986694336,
 55.83853149414063,
 53.51207160949707,
 54.82537269592285,
 53.569284439086914,
 53.5296745300293,
 54.354637145996094,
 54.313310623168945,
 53.26720809936523,
 54.64541053771973,
 55.00912475585938,
 55.093666076660156,
 55.138763427734375,
 54.19987297058106,
 54.07197189331055,
 53.18226623535156,
 53.656246185302734,
 54.97188377380371,
 55.28757095336914,
 54.08882141113281,
 53.08153915405274,
 53.61944770812988,
 53.15986633300781,
 53.53702735900879,
 53.32623863220215,
 52.01462173461914]
passive= [54.46392059326172,
 52.37292861938477,
 51.95756149291992,
 53.40110778808594,
 54.46831512451172,
 56.04657173156738,
 57.74487495422363,
 53.75052452087402,
 56.246402740478516,
 55.15713691711426]

My current output is a list of 10.I want a list of 40.I want to take divide the first four elements of active with first element of passive...and so on.In the end I want a new list of 40 elements and not 10. example [active1/passive1,active2/passive1,active3/passive1....active40/passive10]

[[1.0454586587604597,
  0.9936967470945319,
  1.0078963662125922,
  1.0137739110579735],
 [1.0366928664488493,
  1.0274097658218595,
  1.0399177333006342,
  1.040568497228071],
 [1.020384882546816,
  1.0368647296698332,
  1.0230744951511204,
  1.0240563338877238],
 [0.9836830066620091,
  1.0492516490722763,
  1.013507569945381,
  1.045643691807429],
 [0.9824440408551517,
  1.0065553261670555,
  0.9834944282126284,
  0.982767218109524],
 [0.9698119879004422,
  0.9690746274955083,
  0.9504097477092123,
  0.9750000553011796],
 [0.9526234977470646,
  0.954087546649548,
  0.9548685224696527,
  0.9386092361191739],
 [1.005980357871888,
  0.9894278559960512,
  0.9982460015709302,
  1.0227227411047006],
 [0.9829530113857514,
  0.9616405454531767,
  0.9437321600631335,
  0.9532955903958973],
 [0.9637894441999811,
  0.9706273811757119,
  0.9668057773255447,
  0.9430261366318299]]


Solution

  • For e.g.

    a = range(40)
    b = range(10)
    

    Simplest:

    [x * b[i//4] for i, x in enumerate(a)]
    

    More functional:

    # from https://docs.python.org/3/library/itertools.html#itertools-recipes
    from itertools import zip_longest
    def grouper(iterable, n, fillvalue=None):
        "Collect data into fixed-length chunks or blocks"
        # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
        args = [iter(iterable)] * n
        return zip_longest(*args, fillvalue=fillvalue)
    
    [a1 * b1 for a4, b1 in zip(grouper(a, 4), b) for a1 in a4]