Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin.stu.neva.ru!news.astraweb.com!border5.a.newsrouter.astraweb.com!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed3.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'initialize': 0.05; 'data:': 0.07; 'f.close()': 0.07; 'python': 0.09; '"r")': 0.09; 'advice.': 0.09; 'array.': 0.09; 'idea?': 0.09; 'subject:using': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'bigger.': 0.16; 'buffering,': 0.16; 'columns': 0.16; 'idea:': 0.16; 'numpy': 0.16; 'row': 0.16; 'subject:array': 0.16; 'subject:when': 0.16; 'wrote:': 0.17; 'file.': 0.20; 'all,': 0.21; 'bit': 0.21; 'import': 0.21; 'libraries': 0.22; 'wednesday,': 0.22; 'nearly': 0.23; 'second': 0.24; 'cc:2**1': 0.24; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:[ 10': 0.26; 'raw': 0.27; 'helpful.': 0.29; 'idea,': 0.29; 'loop,': 0.29; 'once.': 0.29; 'array': 0.29; 'problem.': 0.32; 'file': 0.32; 'print': 0.32; 'guys': 0.33; 'likely': 0.33; 'progress': 0.33; 'times.': 0.33; 'skip:b 20': 0.34; 'received:google.com': 0.34; 'thanks': 0.34; 'list': 0.35; 'doing': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'really': 0.36; 'but': 0.36; 'cc:no real name:2**1': 0.36; 'one,': 0.37; 'two': 0.37; 'why': 0.37; 'rather': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'some': 0.38; 'several': 0.39; 'description': 0.39; 'received:209.85.214': 0.39; 'where': 0.40; 'help': 0.40; 'your': 0.60; 'great': 0.64; '2013': 0.84; 'isaac': 0.84; 'inefficient': 0.91; 'angel': 0.93; 'successful.': 0.93; 'won': 0.96 Newsgroups: comp.lang.python Date: Wed, 2 Jan 2013 18:41:46 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=129.118.0.2; posting-account=DOyQBwoAAADIEpwWr89vk1x7RGFM33oc References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 129.118.0.2 MIME-Version: 1.0 Subject: Re: avoding the accumulation of array when using loop. From: Isaac Won To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org, d@davea.name 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: , Message-ID: Lines: 158 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1357180915 news.xs4all.nl 6853 [2001:888:2000:d::a6]:40911 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:36025 On Wednesday, January 2, 2013 5:54:18 PM UTC-6, Dave Angel wrote: > On 01/02/2013 05:21 PM, Isaac Won wrote: >=20 > > Hi all, >=20 > > >=20 > > Thanks to Hans, I have had a good progress on my problem.=20 >=20 > > >=20 > > Followings are Hans's Idea: >=20 > > >=20 > > import numpy as np=20 >=20 > > >=20 > > b =3D []=20 >=20 > > c =3D 4=20 >=20 > > f =3D open("text.file", "r")=20 >=20 > > >=20 > > while c < 10:=20 >=20 > > c =3D c + 1=20 >=20 > > >=20 > > >=20 > > f.seek(0,0)=20 >=20 > > >=20 > > for columns in ( raw.strip().split() for raw in f ):=20 >=20 > > b.append(columns[c])=20 >=20 > > >=20 > > y =3D np.array(b, float)=20 >=20 > > print c, y=20 >=20 > > >=20 > > >=20 > > It's a bit inefficient to read the same file several times.=20 >=20 >=20 >=20 > Don't bet on it. The OS and the libraries and Python each do some >=20 > buffering, so it might be nearly as fast to just reread if it's a small >=20 > file. And if it's a huge one, the list would be even bigger. So the >=20 > only sizes where the second approach is likely better is the mid-size fil= e. >=20 >=20 >=20 > > You might consider reading it just once. For example:=20 >=20 > > >=20 > > >=20 > > import numpy as np=20 >=20 > > >=20 > > b =3D []=20 >=20 > > >=20 > > >=20 > > >=20 > > f =3D open("text.file", "r")=20 >=20 > > >=20 > > data =3D [ line.strip().split() for line in f ]=20 >=20 > > f.close()=20 >=20 > > >=20 > > for c in xrange(5, 11):=20 >=20 > > for row in data:=20 >=20 > > b.append(row[c])=20 >=20 > > >=20 > > >=20 > > y =3D np.array(b, float)=20 >=20 > > print c, y=20 >=20 > > -----------------------------------------------------------------------= -------- >=20 > > >=20 > > It is a great idea, but I found some problems. I want each individual a= rray of y. However, these two codes prodce accumulated array such as [1,2,3= ], [1,2,3,4,5,6], [1,2,3,4,5,6,7,8,9] and so on. I have tried to initialize= for loop for each time to produce array. This effort has not been very suc= cessful. >=20 > > Do you guys have any idea? I will really appreciate ant help and idea. >=20 >=20 >=20 > Your description is very confusing. But i don't see why you just don't >=20 > just set b=3D[] inside the outer loop, rather than doing it at the begin >=20 > of the program. >=20 >=20 >=20 > for c in xrange(5, 11):=20 >=20 > b =3D [] >=20 > for row in data:=20 >=20 > b.append(row[c])=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 > --=20 >=20 >=20 >=20 > DaveA Hi Dave, I really appreciate your advice. It was really helpful. Isaac