Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; '21,': 0.07; 'error:': 0.07; 'parser': 0.07; '22,': 0.09; '34,': 0.09; '40,': 0.09; '51,': 0.09; '75,': 0.09; 'back.': 0.09; 'feasible.': 0.09; 'overflow': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'suggest': 0.14; '"["': 0.16; '###': 0.16; '105,': 0.16; '120,': 0.16; '122,': 0.16; '23,': 0.16; '24,': 0.16; '32,': 0.16; '33,': 0.16; '36,': 0.16; '37,': 0.16; '39,': 0.16; '42,': 0.16; '46,': 0.16; '49,': 0.16; '52,': 0.16; '54,': 0.16; '55,': 0.16; '57,': 0.16; '58,': 0.16; '60,': 0.16; '61,': 0.16; '63,': 0.16; '64,': 0.16; '66,': 0.16; '67,': 0.16; '70,': 0.16; '89,': 0.16; 'ast': 0.16; 'compiler.': 0.16; 'filename,': 0.16; 'mode,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'reproduce': 0.16; 'wrote:': 0.18; 'stack': 0.19; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; '31,': 0.24; 'parse': 0.24; 'simpler': 0.24; 'fine': 0.24; 'source': 0.25; '15,': 0.26; 'skip:" 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'returned': 0.30; 'code': 0.31; '"",': 0.31; '13,': 0.31; '25,': 0.31; '50,': 0.31; 'produces': 0.31; 'workaround': 0.31; 'file': 0.32; 'lists': 0.32; '(most': 0.33; 'cases': 0.33; 'limitation': 0.33; 'skip:b 30': 0.33; 'could': 0.34; 'convert': 0.35; 'there': 0.35; '14,': 0.36; 'leads': 0.36; 'subject:data': 0.36; 'thanks': 0.36; 'similar': 0.36; 'two': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; 'recent': 0.39; 'explain': 0.39; '12,': 0.39; 'skip:b 40': 0.39; 'to:addr:python.org': 0.39; 'skip:- 60': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'experts': 0.60; 'tell': 0.60; 'kindly': 0.61; 'skip:n 10': 0.64; '30,': 0.65; '20,': 0.68; '26,': 0.68; 'skip:a 40': 0.72; '71,': 0.84; '74,': 0.84; '81,': 0.84; '88,': 0.84; '92,': 0.84; '95,': 0.84; 'advances.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: MemoryError in data conversion Date: Mon, 14 Apr 2014 09:46:48 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bda2ce.dip0.t-ipconnect.de User-Agent: KNode/4.11.5 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 90 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1397461622 news.xs4all.nl 2935 [2001:888:2000:d::a6]:37292 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70208 Mok-Kong Shen wrote: > The code attached below produces in one of the two IMHO similar cases > (excepting the sizes of the lists involved) MemoryError. Could experts > kindly tell why that's so and whether there is any work-around feasible. Here's a simpler way to reproduce the error: >>> import ast >>> def nested_list_literal(n): ... return "[" * n + "42" + "]" * n ... >>> ast.literal_eval(nested_list_literal(98)) [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[42]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] >>> ast.literal_eval(nested_list_literal(99)) s_push: parser stack overflow Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.3/ast.py", line 47, in literal_eval node_or_string = parse(node_or_string, mode='eval') File "/usr/lib/python3.3/ast.py", line 35, in parse return compile(source, filename, mode, PyCF_ONLY_AST) MemoryError You ran into a limitation of the compiler. For us to suggest a workaround you'd have to explain why you want to convert the list returned from buildhuffmantree() into python source code and back. > Thanks in advances. > > M. K. Shen > > ----------------------------------------------------------------- > > import ast > > def buildhuffmantree(slist,flist): > item=slist[:] > freq=flist[:] > while len(item)>2: > mn=min(freq) > id=freq.index(mn) > u=item[id] > del item[id] > del freq[id] > mn1=min(freq) > id=freq.index(mn1) > v=item[id] > del item[id] > del freq[id] > item.append([u,v]) > freq.append(mn+mn1) > return(item) > > def processing(slist,flist): > bintree=buildhuffmantree(slist,flist) > print(bintree) > byarray=bytearray(str(bintree),"latin-1") > bintree1=ast.literal_eval(byarray.decode("latin-1")) > print(bintree1) > print(bintree==bintree1) > > slist1=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 'eof'] > > flist1=[18, 16, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, -1] > > slist2=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, > 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, > 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, > 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, > 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, > 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, > 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, > 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, > 124, 125, 126, 127, 'eof'] > > flist2=[2, 2, 0, 2, 0, 0, 1, 2, 1, 0, 2, 0, 0, 1, 1, 0, 2, 0, 0, 0, 1, > 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, > 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, > 0, 1, 0, 2, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, > 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, > 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, > 0, 0, -1] > > processing(slist1,flist1) ### This works fine > print() > > processing(slist2,flist2) ### This leads to MemoryError