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


Groups > comp.lang.python > #36012 > unrolled thread

avoding the accumulation of array when using loop.

Started byIsaac Won <winefrog@gmail.com>
First post2013-01-02 14:21 -0800
Last post2013-01-02 18:41 -0800
Articles 4 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  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

#36012 — avoding the accumulation of array when using loop.

FromIsaac Won <winefrog@gmail.com>
Date2013-01-02 14:21 -0800
Subjectavoding the accumulation of array when using loop.
Message-ID<d00e1c2c-56a8-4d19-ba4b-da4d3281e275@googlegroups.com>
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. 
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.

Thanks,

Isaac

[toc] | [next] | [standalone]


#36016

FromDave Angel <d@davea.name>
Date2013-01-02 18:54 -0500
Message-ID<mailman.5.1357170888.2939.python-list@python.org>
In reply to#36012
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

[toc] | [prev] | [next] | [standalone]


#36024

FromIsaac Won <winefrog@gmail.com>
Date2013-01-02 18:41 -0800
Message-ID<746cf8a0-94c0-43c2-9a59-2272b390f12e@googlegroups.com>
In reply to#36016
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

[toc] | [prev] | [next] | [standalone]


#36025

FromIsaac Won <winefrog@gmail.com>
Date2013-01-02 18:41 -0800
Message-ID<mailman.8.1357180915.2939.python-list@python.org>
In reply to#36016
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web