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?
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()
python python-3.x list
marked as duplicate by Martijn Pieters♦
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.
|
show 6 more comments
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()
python python-3.x list
marked as duplicate by Martijn Pieters♦
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.productis 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 yourproductiterator, i.e. you didlst = [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 withlst = itertools.product([0, 1], repeat=n)and maybe choose another name other thanlstsince it isn't a list.
– juanpa.arrivillaga
Mar 8 at 17:26
|
show 6 more comments
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()
python python-3.x list
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
python python-3.x list
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♦
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♦
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.productis 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 yourproductiterator, i.e. you didlst = [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 withlst = itertools.product([0, 1], repeat=n)and maybe choose another name other thanlstsince it isn't a list.
– juanpa.arrivillaga
Mar 8 at 17:26
|
show 6 more comments
2
itertools.productis 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 yourproductiterator, i.e. you didlst = [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 withlst = itertools.product([0, 1], repeat=n)and maybe choose another name other thanlstsince 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
|
show 6 more comments
1 Answer
1
active
oldest
votes
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.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 8 at 17:35
Jay-PiJay-Pi
726
726
add a comment |
add a comment |
2
itertools.productis 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
productiterator, i.e. you didlst = [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 withlst = itertools.product([0, 1], repeat=n)and maybe choose another name other thanlstsince it isn't a list.– juanpa.arrivillaga
Mar 8 at 17:26