Python Range Memory Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring
Python Memory Error Increase Memory
developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question overflowerror: python int too large to convert to c long x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; memory error python it only takes a minute: Sign up My Python for loop is causing a MemoryError. How can I optimize this? up vote 3 down vote favorite I'm trying to compile a list of all the MAC address Apple devices
Python Range Vs Xrange
will have. oui.txt tells me Apple has been assigned 77 MAC ranges to use. These ranges come in the form of: 00:00:00 00:11:11 etc... This leaves me the last three HEX digits to append. That's 16^6. A total of 1291845632 Apple MAC addresses. The problem I'm having is writing a program to create a list of these MAC addresses. Here's my current code: import re apple_mac_range = [] apple_macs = [] # Parse the HTML of http://standards.ieee.org/cgi-bin/ouisearch to get
Python Xrange
the MACs with open('apple mac list', 'r') as f: for line in f.readlines(): match = re.search(r'[\w\d]{2}-[\w\d]{2}-[\w\d]{2}', line) if match: apple_mac_range.append(match.group().split('-')) for mac in apple_mac_range: for i in range(1, 1291845633): print i This gives me a MemoryError... How can I optimize it? python optimization memory out-of-memory share|improve this question edited Jan 23 '13 at 22:15 Oz123 8,940124491 asked Dec 10 '10 at 2:16 dave 2,46684589 2 Why on earth do you want to generate a complete list of all those MAC addresses? –Jim Garrison Dec 10 '10 at 2:20 1 Why do you need to enumerate over one billion addresses? What is the problem you are really trying to solve? –SingleNegationElimination Dec 10 '10 at 2:21 Well, I really only want iPhone device MACs but, from what I've found so far, Apple doesn't seem to have a specific range(s) they use for certain devices. –dave Dec 10 '10 at 2:26 1 That's not answering the question. Why do you need these MACs? Why do you need to enumerate them ahead of time? What are you going to do with them? –Karl Knechtel Dec 10 '10 at 5:11 add a comment| 5 Answers 5 active oldest votes up vote 17 down vote range(1, 1291845633) creates a list of 1,291,845,632 elements (several GB) all at once. Use xrange(1, 1291845633) instead and it will generate elements as you need them instead of all at on
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Memory Error Python up vote http://stackoverflow.com/questions/4405083/my-python-for-loop-is-causing-a-memoryerror-how-can-i-optimize-this -1 down vote favorite I am trying to process a Monte Carlo Algorithm to calculate the value of pi. When I try to give a large input as mentioned in the program below. I get a memory error. What should i do to rectify it? The code is a s follows: def PiCalc(): N_ok=0 n=1000000000000 for i in range(n): x=random() y=random() if sqrt(x**2 + y**2) http://stackoverflow.com/questions/32720865/memory-error-python <= 1: N_ok+=1 pi= 4.0 * N_ok / n print "No. of points inside the circle: ", N_ok print "Pi: ", pi return pi python python-2.7 montecarlo share|improve this question edited Sep 22 '15 at 15:30 Andrea Corbellini 8,56211935 asked Sep 22 '15 at 15:24 Sandy.Arv 19210 Can you post the output you're seeing? True memory errors in python are rare. –Alex Sep 22 '15 at 15:26 6 Does it help if you use xrange instead of range? –bereal Sep 22 '15 at 15:27 You're not actually using i. –Peter Wood Sep 22 '15 at 15:30 1 Why do you need the sqrt? –Peter Wood Sep 22 '15 at 15:34 1 @Sandy.Arv consider picking and accepting the answer –Amir Sep 22 '15 at 15:42 | show 3 more comments 4 Answers 4 active oldest votes up vote 5 down vote accepted Given that nobody mentioned this... You can also use itertools.repeat(None, n). This will be much faster than both range and xrange, because it won't create one trilion new int instances. With repeat(), your for-loop becomes this: for _ in itertools.repeat(None, n): x=random() y=random() if x**2 + y*
tour help Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and http://programmers.stackexchange.com/questions/269961/facing-memoryerror-in-python policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Software Engineering Questions Tags Users Badges Unanswered Ask Question _ Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle who care about creating, delivering, and memory error maintaining software responsibly. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Facing MemoryError in Python up vote 0 down vote favorite I wrote a program to find prime factors of a number. When I give a large number(600851475143) as python range memory input, MemoryError pops up. Below is the code: def fact(a): factors = [] for i in range(1,a+1): if a%i == 0: factors.append(i) return factors num = raw_input(">> ") #600851475143 a = b = [] a = fact(long(num)) for i in range(0,len(a)): b = fact(a[i]) if len(b) <= 2: print a[i] From browsing I came to know that Python makes use of Computer memory - RAM. I am using Python in Unbuntu without changing its configuration. Should I change anythig to work on 64-bit machine. Or should I use any additional function(s) to get around this error python memory error-messages share|improve this question asked Jan 13 '15 at 18:47 Gdek 313 4 Your basic issue is that your algorithm is likely using too much memory. If you are using python 2, then range(1, a+1) is attempting to create a list with 600851475143 elements. This is probably not what you want as each element will be an integer and each integer takes 4 bytes. (Also, this question isn't appropriate for Programmers as you really need a code review and to understand in particular how python work