Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #36025

Re: avoding the accumulation of array when using loop.

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 <winefrog@gmail.com>
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 <mailman.5.1357170888.2939.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=129.118.0.2; posting-account=DOyQBwoAAADIEpwWr89vk1x7RGFM33oc
References <d00e1c2c-56a8-4d19-ba4b-da4d3281e275@googlegroups.com> <mailman.5.1357170888.2939.python-list@python.org>
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 <winefrog@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.8.1357180915.2939.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Wednesday, January 2, 2013 5:54:18 PM UTC-6, Dave Angel wrote:
> On 01/02/2013 05:21 PM, Isaac Won wrote:
> 
> > Hi all,
> 
> >
> 
> > Thanks to Hans, I have had a good progress on my problem. 
> 
> >
> 
> > Followings are Hans's Idea:
> 
> >
> 
> > import numpy as np 
> 
> >
> 
> > b = [] 
> 
> > c = 4 
> 
> > f = open("text.file", "r") 
> 
> >
> 
> > while c < 10: 
> 
> >         c = c + 1 
> 
> >
> 
> >
> 
> >         f.seek(0,0) 
> 
> >
> 
> >         for  columns in ( raw.strip().split() for raw in f ): 
> 
> >                 b.append(columns[c]) 
> 
> >
> 
> >         y = np.array(b, float) 
> 
> >         print c, y 
> 
> >
> 
> >
> 
> > It's a bit inefficient to read the same file several times. 
> 
> 
> 
> Don't bet on it.  The OS and the libraries and Python each do some
> 
> buffering, so it might be nearly as fast to just reread if it's a small
> 
> file.  And if it's a huge one, the list would be even bigger.  So the
> 
> only sizes where the second approach is likely better is the mid-size file.
> 
> 
> 
> > You might consider reading it just once.  For example: 
> 
> >
> 
> >
> 
> > import numpy as np 
> 
> >
> 
> > b = [] 
> 
> >
> 
> >
> 
> >
> 
> > f = open("text.file", "r") 
> 
> >
> 
> > data = [ line.strip().split() for line in f ] 
> 
> > f.close() 
> 
> >
> 
> > for c in xrange(5, 11): 
> 
> >         for row in data: 
> 
> >                 b.append(row[c]) 
> 
> >
> 
> >
> 
> >         y = np.array(b, float) 
> 
> >         print c, y 
> 
> > -------------------------------------------------------------------------------
> 
> >
> 
> > It is a great idea, but I found some problems. I want each individual array 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 successful.
> 
> > Do you guys have any idea? I will really appreciate ant help and idea.
> 
> 
> 
> Your description is very confusing.  But i don't see why you just don't
> 
> just set b=[] inside the outer loop, rather than doing it at the begin
> 
> of the program.
> 
> 
> 
> for c in xrange(5, 11): 
> 
>         b = []
> 
>         for row in data: 
> 
>                 b.append(row[c]) 
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> 
> 
> DaveA

Hi Dave,

I really appreciate your advice. It was really helpful.

Isaac

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

avoding the accumulation of array when using loop. Isaac Won <winefrog@gmail.com> - 2013-01-02 14:21 -0800
  Re: avoding the accumulation of array when using loop. Dave Angel <d@davea.name> - 2013-01-02 18:54 -0500
    Re: avoding the accumulation of array when using loop. Isaac Won <winefrog@gmail.com> - 2013-01-02 18:41 -0800
    Re: avoding the accumulation of array when using loop. Isaac Won <winefrog@gmail.com> - 2013-01-02 18:41 -0800

csiph-web