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


Groups > comp.lang.python > #36016

Re: avoding the accumulation of array when using loop.

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.109.133.83.MISMATCH!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <d@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'example:': 0.03; 'initialize': 0.05; 'data:': 0.07; 'f.close()': 0.07; 'python': 0.09; '"r")': 0.09; 'array.': 0.09; 'idea?': 0.09; 'subject:using': 0.09; '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; 'nearly': 0.23; 'second': 0.24; 'tried': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:[ 10': 0.26; 'raw': 0.27; 'idea,': 0.29; 'loop,': 0.29; 'once.': 0.29; 'array': 0.29; 'problem.': 0.32; 'file': 0.32; 'print': 0.32; 'to:addr:python-list': 0.33; 'guys': 0.33; 'likely': 0.33; 'progress': 0.33; 'times.': 0.33; 'skip:b 20': 0.34; 'thanks': 0.34; 'list': 0.35; 'doing': 0.35; 'pm,': 0.35; 'really': 0.36; 'but': 0.36; 'one,': 0.37; 'two': 0.37; 'why': 0.37; 'rather': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'some': 0.38; 'several': 0.39; 'description': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'help': 0.40; 'your': 0.60; 'great': 0.64; 'header:Reply- To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'isaac': 0.84; 'received:74.208.4.194': 0.84; 'inefficient': 0.91; 'successful.': 0.93; 'won': 0.96
Date Wed, 02 Jan 2013 18:54:18 -0500
From Dave Angel <d@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1
MIME-Version 1.0
To python-list@python.org
Subject Re: avoding the accumulation of array when using loop.
References <d00e1c2c-56a8-4d19-ba4b-da4d3281e275@googlegroups.com>
In-Reply-To <d00e1c2c-56a8-4d19-ba4b-da4d3281e275@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:pvJG7mUD3Y+g6l7mknry+6v2uLnTs2lpG6fkIUSE36u iu3l1mMoR/9yWw2f1Ow8k8HxEzevfkTQLxFt4/e8g8yeJ+4A2s M4B+GuB5G9G+tERQ06rZfjZg6n2Jx668IPeP7omc8kg8iRi2OS opD7tbO9JiDHBPH0oEKbc0LaIEs7cNy/+VG8WaCMU5LxgrfosE UGMS8BMRNQvgf/qdo/GZCdx/gmLSEA133vnBCzq7TpXI+7HxuQ 2ylO/1rDpd+9o7xBhjkhAizSRjtPY3XVsJxoaUgznCUO3Tm92K S1d3aAFeqfcADxemVFumh1E6aDIyl1TQDa/Sm09PfMHU6czqw= =
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To d@davea.name
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>
Newsgroups comp.lang.python
Message-ID <mailman.5.1357170888.2939.python-list@python.org> (permalink)
Lines 74
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1357170888 news.xs4all.nl 6904 [2001:888:2000:d::a6]:48520
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:36016

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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