Function that multiplies each element of one list with four elements of second list in a loop to get a new listGetting the last element of a listHow do I get the number of elements in a list?Splitting a list into N parts of approximately equal lengthWhy do I get an UnsupportedOperationException when trying to remove an element from a List?Call int() function on every list element?List order manipulating in PythonPython: How do I add a suffix to the end of each list element?Python lists intersection/comparison with (two) loopsMultiply all combinations of two listsHow to efficiently find out the maximum value below a threshold?

Why are < or > required to use /dev/tcp

What are Elsa's reasons for selecting the Holy Grail on behalf of Donovan?

Where's this swanky house and vineyard near a mountain?

Why don't countries like Japan just print more money?

Did the CIA blow up a Siberian pipeline in 1982?

What is appropriate short form for "laboratoires" in French?

Story about hunting giant lizards for hides on privately owned planet

What determines the direction in which motor proteins go?

Can I enter the UK for 24 hours from a Schengen area, holding an Indian passport?

Is it illegal to withhold someone's passport and green card in California?

Designing a magic-compatible polearm

Why does cooking oatmeal starting with cold milk make it creamy?

Constitutionality of U.S. Democratic Presidential Candidate's Supreme Court Suggestion

How to remove this component from PCB

Why does Linux list NVMe drives as /dev/nvme0 instead of /dev/sda?

Does a vocal melody have any rhythmic responsibility to the underlying arrangement in pop music?

Why is it recommended to mix yogurt starter with a small amount of milk before adding to the entire batch?

What is the origin of Scooby-Doo's name?

Hit the Bulls Eye with T in the Center

Identifying unknown map projection for image

Should I include an appendix for inessential, yet related worldbuilding to my story?

Intuition for the role of diffeomorphisms

Why is it easier to balance a non-moving bike standing up than sitting down?

Are all Ringwraiths called Nazgûl in LotR?



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


Getting the last element of a listHow do I get the number of elements in a list?Splitting a list into N parts of approximately equal lengthWhy do I get an UnsupportedOperationException when trying to remove an element from a List?Call int() function on every list element?List order manipulating in PythonPython: How do I add a suffix to the end of each list element?Python lists intersection/comparison with (two) loopsMultiply all combinations of two listsHow to efficiently find out the maximum value below a threshold?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















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











share|improve this question
























  • Sample lists with what you have already tried would boost your chances of getting valid answers.

    – DirtyBit
    Mar 25 at 7:54











  • Can we see some code? Give it a shot.

    – Aran-Fey
    Mar 25 at 7:55











  • Multiply them how? 1*1, 2*1, 3*1, 4*1 or 1*2*3*4*1? (Both lists being natural numbers sequences starting in 1)

    – Netwave
    Mar 25 at 7:58











  • a=[1,2,3,.....40] len(a)=40 b=[1,2,....10] len(b)=10

    – mashedpoteto
    Mar 25 at 8:14











  • I want to make two lists,one by multiplying & other by dividing ->every first four element of a[ ] with the every element of b [ ] in loop. So something like : [1,2,3,4]/1 -> will be the first element of my new list. The second will be [5,6,7,8]/2 and so on.I hope this makes clear. The last element of the new set would be [37,38,39,40]/10. The above example was for division operator.I want to repeat the same thing with multiplication.

    – mashedpoteto
    Mar 25 at 8:19

















0















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











share|improve this question
























  • Sample lists with what you have already tried would boost your chances of getting valid answers.

    – DirtyBit
    Mar 25 at 7:54











  • Can we see some code? Give it a shot.

    – Aran-Fey
    Mar 25 at 7:55











  • Multiply them how? 1*1, 2*1, 3*1, 4*1 or 1*2*3*4*1? (Both lists being natural numbers sequences starting in 1)

    – Netwave
    Mar 25 at 7:58











  • a=[1,2,3,.....40] len(a)=40 b=[1,2,....10] len(b)=10

    – mashedpoteto
    Mar 25 at 8:14











  • I want to make two lists,one by multiplying & other by dividing ->every first four element of a[ ] with the every element of b [ ] in loop. So something like : [1,2,3,4]/1 -> will be the first element of my new list. The second will be [5,6,7,8]/2 and so on.I hope this makes clear. The last element of the new set would be [37,38,39,40]/10. The above example was for division operator.I want to repeat the same thing with multiplication.

    – mashedpoteto
    Mar 25 at 8:19













0












0








0








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











share|improve this question
















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








python list multiplying






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 8:52







mashedpoteto

















asked Mar 25 at 7:53









mashedpotetomashedpoteto

105




105












  • Sample lists with what you have already tried would boost your chances of getting valid answers.

    – DirtyBit
    Mar 25 at 7:54











  • Can we see some code? Give it a shot.

    – Aran-Fey
    Mar 25 at 7:55











  • Multiply them how? 1*1, 2*1, 3*1, 4*1 or 1*2*3*4*1? (Both lists being natural numbers sequences starting in 1)

    – Netwave
    Mar 25 at 7:58











  • a=[1,2,3,.....40] len(a)=40 b=[1,2,....10] len(b)=10

    – mashedpoteto
    Mar 25 at 8:14











  • I want to make two lists,one by multiplying & other by dividing ->every first four element of a[ ] with the every element of b [ ] in loop. So something like : [1,2,3,4]/1 -> will be the first element of my new list. The second will be [5,6,7,8]/2 and so on.I hope this makes clear. The last element of the new set would be [37,38,39,40]/10. The above example was for division operator.I want to repeat the same thing with multiplication.

    – mashedpoteto
    Mar 25 at 8:19

















  • Sample lists with what you have already tried would boost your chances of getting valid answers.

    – DirtyBit
    Mar 25 at 7:54











  • Can we see some code? Give it a shot.

    – Aran-Fey
    Mar 25 at 7:55











  • Multiply them how? 1*1, 2*1, 3*1, 4*1 or 1*2*3*4*1? (Both lists being natural numbers sequences starting in 1)

    – Netwave
    Mar 25 at 7:58











  • a=[1,2,3,.....40] len(a)=40 b=[1,2,....10] len(b)=10

    – mashedpoteto
    Mar 25 at 8:14











  • I want to make two lists,one by multiplying & other by dividing ->every first four element of a[ ] with the every element of b [ ] in loop. So something like : [1,2,3,4]/1 -> will be the first element of my new list. The second will be [5,6,7,8]/2 and so on.I hope this makes clear. The last element of the new set would be [37,38,39,40]/10. The above example was for division operator.I want to repeat the same thing with multiplication.

    – mashedpoteto
    Mar 25 at 8:19
















Sample lists with what you have already tried would boost your chances of getting valid answers.

– DirtyBit
Mar 25 at 7:54





Sample lists with what you have already tried would boost your chances of getting valid answers.

– DirtyBit
Mar 25 at 7:54













Can we see some code? Give it a shot.

– Aran-Fey
Mar 25 at 7:55





Can we see some code? Give it a shot.

– Aran-Fey
Mar 25 at 7:55













Multiply them how? 1*1, 2*1, 3*1, 4*1 or 1*2*3*4*1? (Both lists being natural numbers sequences starting in 1)

– Netwave
Mar 25 at 7:58





Multiply them how? 1*1, 2*1, 3*1, 4*1 or 1*2*3*4*1? (Both lists being natural numbers sequences starting in 1)

– Netwave
Mar 25 at 7:58













a=[1,2,3,.....40] len(a)=40 b=[1,2,....10] len(b)=10

– mashedpoteto
Mar 25 at 8:14





a=[1,2,3,.....40] len(a)=40 b=[1,2,....10] len(b)=10

– mashedpoteto
Mar 25 at 8:14













I want to make two lists,one by multiplying & other by dividing ->every first four element of a[ ] with the every element of b [ ] in loop. So something like : [1,2,3,4]/1 -> will be the first element of my new list. The second will be [5,6,7,8]/2 and so on.I hope this makes clear. The last element of the new set would be [37,38,39,40]/10. The above example was for division operator.I want to repeat the same thing with multiplication.

– mashedpoteto
Mar 25 at 8:19





I want to make two lists,one by multiplying & other by dividing ->every first four element of a[ ] with the every element of b [ ] in loop. So something like : [1,2,3,4]/1 -> will be the first element of my new list. The second will be [5,6,7,8]/2 and so on.I hope this makes clear. The last element of the new set would be [37,38,39,40]/10. The above example was for division operator.I want to repeat the same thing with multiplication.

– mashedpoteto
Mar 25 at 8:19












2 Answers
2






active

oldest

votes


















0














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]





share|improve this answer

























  • I just posted a sample comment above,I tried this out but I am unfortunately not getting the desired result.

    – mashedpoteto
    Mar 25 at 8:21











  • @mashedpoteto Looks like the desired result to me. If this isn't what you want, show us what is that you want and explain how this is different from that. (And don't do it in comments... edit it into your question.)

    – Aran-Fey
    Mar 25 at 8:27












  • @Aran-Fey edited it :) Can you take a look?

    – mashedpoteto
    Mar 25 at 8:53











  • Rolled back because the first solution was actually the correct one (yielding a list of 40 numbers, not a list 10 lists of 4); "Not getting the desired result" - you should be more specific how the "desired result" differs from the one obtained by my code.

    – Amadan
    Mar 25 at 8:56












  • @Amadan it worked.Initially the problem was a very simple error on how I wrote the code,which gave a division by zero error.Thank you :)

    – mashedpoteto
    Mar 26 at 12:14


















0














You can do it using pure Python in one line:



newList = [active[j] / passive[i] for i in range(len(passive)) for j in range(i*4,i*4+4)]
for elem in newList:
print(elem)


This will print:



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


Same goes for multiplying:



newList = [active[j] * passive[i] for i in range(len(passive)) for j in range(i*4,i*4+4)]





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55333315%2ffunction-that-multiplies-each-element-of-one-list-with-four-elements-of-second-l%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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]





    share|improve this answer

























    • I just posted a sample comment above,I tried this out but I am unfortunately not getting the desired result.

      – mashedpoteto
      Mar 25 at 8:21











    • @mashedpoteto Looks like the desired result to me. If this isn't what you want, show us what is that you want and explain how this is different from that. (And don't do it in comments... edit it into your question.)

      – Aran-Fey
      Mar 25 at 8:27












    • @Aran-Fey edited it :) Can you take a look?

      – mashedpoteto
      Mar 25 at 8:53











    • Rolled back because the first solution was actually the correct one (yielding a list of 40 numbers, not a list 10 lists of 4); "Not getting the desired result" - you should be more specific how the "desired result" differs from the one obtained by my code.

      – Amadan
      Mar 25 at 8:56












    • @Amadan it worked.Initially the problem was a very simple error on how I wrote the code,which gave a division by zero error.Thank you :)

      – mashedpoteto
      Mar 26 at 12:14















    0














    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]





    share|improve this answer

























    • I just posted a sample comment above,I tried this out but I am unfortunately not getting the desired result.

      – mashedpoteto
      Mar 25 at 8:21











    • @mashedpoteto Looks like the desired result to me. If this isn't what you want, show us what is that you want and explain how this is different from that. (And don't do it in comments... edit it into your question.)

      – Aran-Fey
      Mar 25 at 8:27












    • @Aran-Fey edited it :) Can you take a look?

      – mashedpoteto
      Mar 25 at 8:53











    • Rolled back because the first solution was actually the correct one (yielding a list of 40 numbers, not a list 10 lists of 4); "Not getting the desired result" - you should be more specific how the "desired result" differs from the one obtained by my code.

      – Amadan
      Mar 25 at 8:56












    • @Amadan it worked.Initially the problem was a very simple error on how I wrote the code,which gave a division by zero error.Thank you :)

      – mashedpoteto
      Mar 26 at 12:14













    0












    0








    0







    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]





    share|improve this answer















    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]






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 25 at 8:55

























    answered Mar 25 at 7:58









    AmadanAmadan

    138k13152203




    138k13152203












    • I just posted a sample comment above,I tried this out but I am unfortunately not getting the desired result.

      – mashedpoteto
      Mar 25 at 8:21











    • @mashedpoteto Looks like the desired result to me. If this isn't what you want, show us what is that you want and explain how this is different from that. (And don't do it in comments... edit it into your question.)

      – Aran-Fey
      Mar 25 at 8:27












    • @Aran-Fey edited it :) Can you take a look?

      – mashedpoteto
      Mar 25 at 8:53











    • Rolled back because the first solution was actually the correct one (yielding a list of 40 numbers, not a list 10 lists of 4); "Not getting the desired result" - you should be more specific how the "desired result" differs from the one obtained by my code.

      – Amadan
      Mar 25 at 8:56












    • @Amadan it worked.Initially the problem was a very simple error on how I wrote the code,which gave a division by zero error.Thank you :)

      – mashedpoteto
      Mar 26 at 12:14

















    • I just posted a sample comment above,I tried this out but I am unfortunately not getting the desired result.

      – mashedpoteto
      Mar 25 at 8:21











    • @mashedpoteto Looks like the desired result to me. If this isn't what you want, show us what is that you want and explain how this is different from that. (And don't do it in comments... edit it into your question.)

      – Aran-Fey
      Mar 25 at 8:27












    • @Aran-Fey edited it :) Can you take a look?

      – mashedpoteto
      Mar 25 at 8:53











    • Rolled back because the first solution was actually the correct one (yielding a list of 40 numbers, not a list 10 lists of 4); "Not getting the desired result" - you should be more specific how the "desired result" differs from the one obtained by my code.

      – Amadan
      Mar 25 at 8:56












    • @Amadan it worked.Initially the problem was a very simple error on how I wrote the code,which gave a division by zero error.Thank you :)

      – mashedpoteto
      Mar 26 at 12:14
















    I just posted a sample comment above,I tried this out but I am unfortunately not getting the desired result.

    – mashedpoteto
    Mar 25 at 8:21





    I just posted a sample comment above,I tried this out but I am unfortunately not getting the desired result.

    – mashedpoteto
    Mar 25 at 8:21













    @mashedpoteto Looks like the desired result to me. If this isn't what you want, show us what is that you want and explain how this is different from that. (And don't do it in comments... edit it into your question.)

    – Aran-Fey
    Mar 25 at 8:27






    @mashedpoteto Looks like the desired result to me. If this isn't what you want, show us what is that you want and explain how this is different from that. (And don't do it in comments... edit it into your question.)

    – Aran-Fey
    Mar 25 at 8:27














    @Aran-Fey edited it :) Can you take a look?

    – mashedpoteto
    Mar 25 at 8:53





    @Aran-Fey edited it :) Can you take a look?

    – mashedpoteto
    Mar 25 at 8:53













    Rolled back because the first solution was actually the correct one (yielding a list of 40 numbers, not a list 10 lists of 4); "Not getting the desired result" - you should be more specific how the "desired result" differs from the one obtained by my code.

    – Amadan
    Mar 25 at 8:56






    Rolled back because the first solution was actually the correct one (yielding a list of 40 numbers, not a list 10 lists of 4); "Not getting the desired result" - you should be more specific how the "desired result" differs from the one obtained by my code.

    – Amadan
    Mar 25 at 8:56














    @Amadan it worked.Initially the problem was a very simple error on how I wrote the code,which gave a division by zero error.Thank you :)

    – mashedpoteto
    Mar 26 at 12:14





    @Amadan it worked.Initially the problem was a very simple error on how I wrote the code,which gave a division by zero error.Thank you :)

    – mashedpoteto
    Mar 26 at 12:14













    0














    You can do it using pure Python in one line:



    newList = [active[j] / passive[i] for i in range(len(passive)) for j in range(i*4,i*4+4)]
    for elem in newList:
    print(elem)


    This will print:



    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


    Same goes for multiplying:



    newList = [active[j] * passive[i] for i in range(len(passive)) for j in range(i*4,i*4+4)]





    share|improve this answer



























      0














      You can do it using pure Python in one line:



      newList = [active[j] / passive[i] for i in range(len(passive)) for j in range(i*4,i*4+4)]
      for elem in newList:
      print(elem)


      This will print:



      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


      Same goes for multiplying:



      newList = [active[j] * passive[i] for i in range(len(passive)) for j in range(i*4,i*4+4)]





      share|improve this answer

























        0












        0








        0







        You can do it using pure Python in one line:



        newList = [active[j] / passive[i] for i in range(len(passive)) for j in range(i*4,i*4+4)]
        for elem in newList:
        print(elem)


        This will print:



        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


        Same goes for multiplying:



        newList = [active[j] * passive[i] for i in range(len(passive)) for j in range(i*4,i*4+4)]





        share|improve this answer













        You can do it using pure Python in one line:



        newList = [active[j] / passive[i] for i in range(len(passive)) for j in range(i*4,i*4+4)]
        for elem in newList:
        print(elem)


        This will print:



        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


        Same goes for multiplying:



        newList = [active[j] * passive[i] for i in range(len(passive)) for j in range(i*4,i*4+4)]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 25 at 10:19









        Vasilis G.Vasilis G.

        4,3462925




        4,3462925



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55333315%2ffunction-that-multiplies-each-element-of-one-list-with-four-elements-of-second-l%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현