How to efficiently handle large list in Python3? [duplicate] The Next CEO of Stack OverflowHow to find all combinations of coins when given some dollar valueHow do I check if a list is empty?How do I check whether a file exists without exceptions?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow do you split a list into evenly sized chunks?How to make a flat list out of list of lists?How do I get the number of elements in a list in Python?How do I concatenate two lists in Python?How to clone or copy a list?How do I list all files of a directory?

"Eavesdropping" vs "Listen in on"

Can this note be analyzed as a non-chord tone?

Physiological effects of huge anime eyes

Does destroying a Lich's phylactery destroy the soul within it?

IC has pull-down resistors on SMBus lines?

What does "shotgun unity" refer to here in this sentence?

Can I calculate next year's exemptions based on this year's refund/amount owed?

Is French Guiana a (hard) EU border?

Decide between Polyglossia and Babel for LuaLaTeX in 2019

What day is it again?

Pulling the principal components out of a DimensionReducerFunction?

Inexact numbers as keys in Association?

Spaces in which all closed sets are regular closed

How did Beeri the Hittite come up with naming his daughter Yehudit?

Is there a way to save my career from absolute disaster?

Yu-Gi-Oh cards in Python 3

Easy to read palindrome checker

Won the lottery - how do I keep the money?

How many extra stops do monopods offer for tele photographs?

Reference request: Grassmannian and Plucker coordinates in type B, C, D

Help/tips for a first time writer?

Expressing the idea of having a very busy time

How to Implement Deterministic Encryption Safely in .NET

How to get the last not-null value in an ordered column of a huge table?



How to efficiently handle large list in Python3? [duplicate]



The Next CEO of Stack OverflowHow to find all combinations of coins when given some dollar valueHow do I check if a list is empty?How do I check whether a file exists without exceptions?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow do you split a list into evenly sized chunks?How to make a flat list out of list of lists?How do I get the number of elements in a list in Python?How do I concatenate two lists in Python?How to clone or copy a list?How do I list all files of a directory?










0
















This question already has an answer here:



  • How to find all combinations of coins when given some dollar value

    34 answers



I was given a weird task to find a product mix out of 33 different products to make a certain total sales value. The possible combination apparently is 2^33. When I used the code below it says "Memory Error". My win10 desktop has 64G memory installed. Good news is that I solved this problem by using Excel VBA, it took few hours to get the result. Does anyone has ideas to deal with this type of problem? Using a generator? or a PD dataframe? Thanks!



import numpy as np
import itertools, sys

a1 = [4435.48, 327.96, 23.26, 4136.78, 77.06, 158.73, 1389.34,
820.32, 888.33, 3735.7, 201.78, 31.17, 250.04, 87.17, 3230.95,
491.53, 47.11, 508.22, 52.22, 1255.98, 3755.11, 948.4, 905.44,
80.41, 1323.68, 528.57, 1474.4, 83.98, 756.87, 310.68, 27.86,]
n = len(a1)
lst = [list(i) for i in itertools.product([0, 1], repeat=n)]

for g in lst:
c = sum(np.multiply(g, a1))
if abs(c - 11833) < 1:
print (g)
sys.exit()









share|improve this question















marked as duplicate by Martijn Pieters list
Users with the  list badge can single-handedly close list questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 8 at 17:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 2





    itertools.product is an iterator - the problem is that you're wrapping it in a list comprehension. Use it iteratively and you shouldn't hit the memory issue.

    – match
    Mar 8 at 17:18












  • You have a variant of the change-making problem, and solutions do not require brute-forcing all combinations.

    – Martijn Pieters
    Mar 8 at 17:21












  • @juanpa.arrivillaga - quite right - just in time for me to edit :)

    – match
    Mar 8 at 17:21











  • Thanks Guys! I am super new to Python. "Use it iteratively" Can you elaborate a little bit? @match.

    – LarryZ
    Mar 8 at 17:25











  • @LarryZ don't materialize a list out of your product iterator, i.e. you did lst = [list(i) for i in itertools.product([0, 1], repeat=n)] you don't need a list, you want to iterate over the values in the iterator. Literally replace that line with lst = itertools.product([0, 1], repeat=n) and maybe choose another name other than lst since it isn't a list.

    – juanpa.arrivillaga
    Mar 8 at 17:26















0
















This question already has an answer here:



  • How to find all combinations of coins when given some dollar value

    34 answers



I was given a weird task to find a product mix out of 33 different products to make a certain total sales value. The possible combination apparently is 2^33. When I used the code below it says "Memory Error". My win10 desktop has 64G memory installed. Good news is that I solved this problem by using Excel VBA, it took few hours to get the result. Does anyone has ideas to deal with this type of problem? Using a generator? or a PD dataframe? Thanks!



import numpy as np
import itertools, sys

a1 = [4435.48, 327.96, 23.26, 4136.78, 77.06, 158.73, 1389.34,
820.32, 888.33, 3735.7, 201.78, 31.17, 250.04, 87.17, 3230.95,
491.53, 47.11, 508.22, 52.22, 1255.98, 3755.11, 948.4, 905.44,
80.41, 1323.68, 528.57, 1474.4, 83.98, 756.87, 310.68, 27.86,]
n = len(a1)
lst = [list(i) for i in itertools.product([0, 1], repeat=n)]

for g in lst:
c = sum(np.multiply(g, a1))
if abs(c - 11833) < 1:
print (g)
sys.exit()









share|improve this question















marked as duplicate by Martijn Pieters list
Users with the  list badge can single-handedly close list questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 8 at 17:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 2





    itertools.product is an iterator - the problem is that you're wrapping it in a list comprehension. Use it iteratively and you shouldn't hit the memory issue.

    – match
    Mar 8 at 17:18












  • You have a variant of the change-making problem, and solutions do not require brute-forcing all combinations.

    – Martijn Pieters
    Mar 8 at 17:21












  • @juanpa.arrivillaga - quite right - just in time for me to edit :)

    – match
    Mar 8 at 17:21











  • Thanks Guys! I am super new to Python. "Use it iteratively" Can you elaborate a little bit? @match.

    – LarryZ
    Mar 8 at 17:25











  • @LarryZ don't materialize a list out of your product iterator, i.e. you did lst = [list(i) for i in itertools.product([0, 1], repeat=n)] you don't need a list, you want to iterate over the values in the iterator. Literally replace that line with lst = itertools.product([0, 1], repeat=n) and maybe choose another name other than lst since it isn't a list.

    – juanpa.arrivillaga
    Mar 8 at 17:26













0












0








0









This question already has an answer here:



  • How to find all combinations of coins when given some dollar value

    34 answers



I was given a weird task to find a product mix out of 33 different products to make a certain total sales value. The possible combination apparently is 2^33. When I used the code below it says "Memory Error". My win10 desktop has 64G memory installed. Good news is that I solved this problem by using Excel VBA, it took few hours to get the result. Does anyone has ideas to deal with this type of problem? Using a generator? or a PD dataframe? Thanks!



import numpy as np
import itertools, sys

a1 = [4435.48, 327.96, 23.26, 4136.78, 77.06, 158.73, 1389.34,
820.32, 888.33, 3735.7, 201.78, 31.17, 250.04, 87.17, 3230.95,
491.53, 47.11, 508.22, 52.22, 1255.98, 3755.11, 948.4, 905.44,
80.41, 1323.68, 528.57, 1474.4, 83.98, 756.87, 310.68, 27.86,]
n = len(a1)
lst = [list(i) for i in itertools.product([0, 1], repeat=n)]

for g in lst:
c = sum(np.multiply(g, a1))
if abs(c - 11833) < 1:
print (g)
sys.exit()









share|improve this question

















This question already has an answer here:



  • How to find all combinations of coins when given some dollar value

    34 answers



I was given a weird task to find a product mix out of 33 different products to make a certain total sales value. The possible combination apparently is 2^33. When I used the code below it says "Memory Error". My win10 desktop has 64G memory installed. Good news is that I solved this problem by using Excel VBA, it took few hours to get the result. Does anyone has ideas to deal with this type of problem? Using a generator? or a PD dataframe? Thanks!



import numpy as np
import itertools, sys

a1 = [4435.48, 327.96, 23.26, 4136.78, 77.06, 158.73, 1389.34,
820.32, 888.33, 3735.7, 201.78, 31.17, 250.04, 87.17, 3230.95,
491.53, 47.11, 508.22, 52.22, 1255.98, 3755.11, 948.4, 905.44,
80.41, 1323.68, 528.57, 1474.4, 83.98, 756.87, 310.68, 27.86,]
n = len(a1)
lst = [list(i) for i in itertools.product([0, 1], repeat=n)]

for g in lst:
c = sum(np.multiply(g, a1))
if abs(c - 11833) < 1:
print (g)
sys.exit()




This question already has an answer here:



  • How to find all combinations of coins when given some dollar value

    34 answers







python python-3.x list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 17:22









juanpa.arrivillaga

39.1k33977




39.1k33977










asked Mar 8 at 17:16









LarryZLarryZ

428




428




marked as duplicate by Martijn Pieters list
Users with the  list badge can single-handedly close list questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 8 at 17:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Martijn Pieters list
Users with the  list badge can single-handedly close list questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 8 at 17:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 2





    itertools.product is an iterator - the problem is that you're wrapping it in a list comprehension. Use it iteratively and you shouldn't hit the memory issue.

    – match
    Mar 8 at 17:18












  • You have a variant of the change-making problem, and solutions do not require brute-forcing all combinations.

    – Martijn Pieters
    Mar 8 at 17:21












  • @juanpa.arrivillaga - quite right - just in time for me to edit :)

    – match
    Mar 8 at 17:21











  • Thanks Guys! I am super new to Python. "Use it iteratively" Can you elaborate a little bit? @match.

    – LarryZ
    Mar 8 at 17:25











  • @LarryZ don't materialize a list out of your product iterator, i.e. you did lst = [list(i) for i in itertools.product([0, 1], repeat=n)] you don't need a list, you want to iterate over the values in the iterator. Literally replace that line with lst = itertools.product([0, 1], repeat=n) and maybe choose another name other than lst since it isn't a list.

    – juanpa.arrivillaga
    Mar 8 at 17:26












  • 2





    itertools.product is an iterator - the problem is that you're wrapping it in a list comprehension. Use it iteratively and you shouldn't hit the memory issue.

    – match
    Mar 8 at 17:18












  • You have a variant of the change-making problem, and solutions do not require brute-forcing all combinations.

    – Martijn Pieters
    Mar 8 at 17:21












  • @juanpa.arrivillaga - quite right - just in time for me to edit :)

    – match
    Mar 8 at 17:21











  • Thanks Guys! I am super new to Python. "Use it iteratively" Can you elaborate a little bit? @match.

    – LarryZ
    Mar 8 at 17:25











  • @LarryZ don't materialize a list out of your product iterator, i.e. you did lst = [list(i) for i in itertools.product([0, 1], repeat=n)] you don't need a list, you want to iterate over the values in the iterator. Literally replace that line with lst = itertools.product([0, 1], repeat=n) and maybe choose another name other than lst since it isn't a list.

    – juanpa.arrivillaga
    Mar 8 at 17:26







2




2





itertools.product is an iterator - the problem is that you're wrapping it in a list comprehension. Use it iteratively and you shouldn't hit the memory issue.

– match
Mar 8 at 17:18






itertools.product is an iterator - the problem is that you're wrapping it in a list comprehension. Use it iteratively and you shouldn't hit the memory issue.

– match
Mar 8 at 17:18














You have a variant of the change-making problem, and solutions do not require brute-forcing all combinations.

– Martijn Pieters
Mar 8 at 17:21






You have a variant of the change-making problem, and solutions do not require brute-forcing all combinations.

– Martijn Pieters
Mar 8 at 17:21














@juanpa.arrivillaga - quite right - just in time for me to edit :)

– match
Mar 8 at 17:21





@juanpa.arrivillaga - quite right - just in time for me to edit :)

– match
Mar 8 at 17:21













Thanks Guys! I am super new to Python. "Use it iteratively" Can you elaborate a little bit? @match.

– LarryZ
Mar 8 at 17:25





Thanks Guys! I am super new to Python. "Use it iteratively" Can you elaborate a little bit? @match.

– LarryZ
Mar 8 at 17:25













@LarryZ don't materialize a list out of your product iterator, i.e. you did lst = [list(i) for i in itertools.product([0, 1], repeat=n)] you don't need a list, you want to iterate over the values in the iterator. Literally replace that line with lst = itertools.product([0, 1], repeat=n) and maybe choose another name other than lst since it isn't a list.

– juanpa.arrivillaga
Mar 8 at 17:26





@LarryZ don't materialize a list out of your product iterator, i.e. you did lst = [list(i) for i in itertools.product([0, 1], repeat=n)] you don't need a list, you want to iterate over the values in the iterator. Literally replace that line with lst = itertools.product([0, 1], repeat=n) and maybe choose another name other than lst since it isn't a list.

– juanpa.arrivillaga
Mar 8 at 17:26












1 Answer
1






active

oldest

votes


















-1














Memory means that you ran out of memory. You can check by import sys; print(sys.maxsize) or import struct; print(struct.calcsize("P")*8).
The latter is the check for 32bit or 64bit.






share|improve this answer





























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    -1














    Memory means that you ran out of memory. You can check by import sys; print(sys.maxsize) or import struct; print(struct.calcsize("P")*8).
    The latter is the check for 32bit or 64bit.






    share|improve this answer



























      -1














      Memory means that you ran out of memory. You can check by import sys; print(sys.maxsize) or import struct; print(struct.calcsize("P")*8).
      The latter is the check for 32bit or 64bit.






      share|improve this answer

























        -1












        -1








        -1







        Memory means that you ran out of memory. You can check by import sys; print(sys.maxsize) or import struct; print(struct.calcsize("P")*8).
        The latter is the check for 32bit or 64bit.






        share|improve this answer













        Memory means that you ran out of memory. You can check by import sys; print(sys.maxsize) or import struct; print(struct.calcsize("P")*8).
        The latter is the check for 32bit or 64bit.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 8 at 17:35









        Jay-PiJay-Pi

        726




        726















            Popular posts from this blog

            How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

            Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme