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


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

Question about nested loop

Started byIsaac Won <winefrog@gmail.com>
First post2012-12-31 02:02 -0800
Last post2012-12-31 14:11 -0800
Articles 6 — 3 participants

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


Contents

  Question about nested loop Isaac Won <winefrog@gmail.com> - 2012-12-31 02:02 -0800
    Re: Question about nested loop Gisle Vanem <gvanem@broadpark.no> - 2012-12-31 12:25 +0100
      Re: Question about nested loop Isaac Won <winefrog@gmail.com> - 2012-12-31 14:02 -0800
      Re: Question about nested loop Isaac Won <winefrog@gmail.com> - 2012-12-31 14:02 -0800
    Re: Question about nested loop Hans Mulder <hansmu@xs4all.nl> - 2012-12-31 13:59 +0100
      Re: Question about nested loop Isaac Won <winefrog@gmail.com> - 2012-12-31 14:11 -0800

#35846 — Question about nested loop

FromIsaac Won <winefrog@gmail.com>
Date2012-12-31 02:02 -0800
SubjectQuestion about nested loop
Message-ID<db8edd31-78f2-4e8c-a6bd-37e1e83ff2ee@googlegroups.com>
Hi all,
I am a very novice for Python. Currently, I am trying to read continuous columns repeatedly in the form of array. 
my code is like below:

import numpy as np

b = []


c = 4
f = open("text.file", "r")


while c < 10:
        c = c + 1

        for  columns in ( raw.strip().split() for raw in f ):


                b.append(columns[c])

        y = np.array(b, float)
        print c, y


I thought that  can get the arrays of the columns[5] to [10], but I only could get repetition of same arrays of columns[5].

The result was something like:

5 [1 2 3 4 ......, 10 9 8]
6 [1 2 3 4 ......, 10 9 8]
7 [1 2 3 4 ......, 10 9 8]
8 [1 2 3 4 ......, 10 9 8]
9 [1 2 3 4 ......, 10 9 8]
10 [1 2 3 4 ......, 10 9 8]


What I can't understand is that even though c increased incrementally upto 10, y arrays stay same.

Would someone help me to understand this problem more?

I really appreciate any help.

Thank you,

Isaac

[toc] | [next] | [standalone]


#35848

FromGisle Vanem <gvanem@broadpark.no>
Date2012-12-31 12:25 +0100
Message-ID<mailman.1497.1356956797.29569.python-list@python.org>
In reply to#35846
"Isaac Won" <winefrog@gmail.com> wrote:

> while c < 10:
>        c = c + 1
> 
>        for  columns in ( raw.strip().split() for raw in f ):
> 
> 
>                b.append(columns[c])
> 
>        y = np.array(b, float)
>        print c, y
> 
> 
> I thought that  can get the arrays of the columns[5] to [10],
> but I only could get repetition of same arrays of columns[5].

I don't pretend to know list comprehension very well, but 
'c' isn't incremented in the inner loop ( .. for raw in f). 
Hence you only append to columns[5].

Maybe you could use another 'd' indexer inside the inner-loop?
But there must a more elegant way to solve your issue. (I'm a
PyCommer myself).

--gv

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


#35856

FromIsaac Won <winefrog@gmail.com>
Date2012-12-31 14:02 -0800
Message-ID<73c7f21d-5bee-4e8b-b415-9adffe7a0da6@googlegroups.com>
In reply to#35848
On Monday, December 31, 2012 5:25:16 AM UTC-6, Gisle Vanem wrote:
> "Isaac Won" <winefrog@gmail.com> wrote:
> 
> 
> 
> > while c < 10:
> 
> >        c = c + 1
> 
> > 
> 
> >        for  columns in ( raw.strip().split() for raw in f ):
> 
> > 
> 
> > 
> 
> >                b.append(columns[c])
> 
> > 
> 
> >        y = np.array(b, float)
> 
> >        print c, y
> 
> > 
> 
> > 
> 
> > I thought that  can get the arrays of the columns[5] to [10],
> 
> > but I only could get repetition of same arrays of columns[5].
> 
> 
> 
> I don't pretend to know list comprehension very well, but 
> 
> 'c' isn't incremented in the inner loop ( .. for raw in f). 
> 
> Hence you only append to columns[5].
> 
> 
> 
> Maybe you could use another 'd' indexer inside the inner-loop?
> 
> But there must a more elegant way to solve your issue. (I'm a
> 
> PyCommer myself).
> 
> 
> 
> --gv

Thank you for your advice.
 I agree with you and tried to increment in inner loop, but still not very succesful. Anyway many thanks for you.

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


#35857

FromIsaac Won <winefrog@gmail.com>
Date2012-12-31 14:02 -0800
Message-ID<mailman.1498.1356991376.29569.python-list@python.org>
In reply to#35848
On Monday, December 31, 2012 5:25:16 AM UTC-6, Gisle Vanem wrote:
> "Isaac Won" <winefrog@gmail.com> wrote:
> 
> 
> 
> > while c < 10:
> 
> >        c = c + 1
> 
> > 
> 
> >        for  columns in ( raw.strip().split() for raw in f ):
> 
> > 
> 
> > 
> 
> >                b.append(columns[c])
> 
> > 
> 
> >        y = np.array(b, float)
> 
> >        print c, y
> 
> > 
> 
> > 
> 
> > I thought that  can get the arrays of the columns[5] to [10],
> 
> > but I only could get repetition of same arrays of columns[5].
> 
> 
> 
> I don't pretend to know list comprehension very well, but 
> 
> 'c' isn't incremented in the inner loop ( .. for raw in f). 
> 
> Hence you only append to columns[5].
> 
> 
> 
> Maybe you could use another 'd' indexer inside the inner-loop?
> 
> But there must a more elegant way to solve your issue. (I'm a
> 
> PyCommer myself).
> 
> 
> 
> --gv

Thank you for your advice.
 I agree with you and tried to increment in inner loop, but still not very succesful. Anyway many thanks for you.

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


#35850

FromHans Mulder <hansmu@xs4all.nl>
Date2012-12-31 13:59 +0100
Message-ID<50e18c36$0$6976$e4fe514c@news2.news.xs4all.nl>
In reply to#35846
On 31/12/12 11:02:56, Isaac Won wrote:
> Hi all,
> I am a very novice for Python. Currently, I am trying to read continuous
> columns repeatedly in the form of array. 
> my code is like below:
> 
> import numpy as np
> 
> b = [] 
> c = 4
> f = open("text.file", "r")
> 
> while c < 10:
>         c = c + 1
> 
>         for  columns in ( raw.strip().split() for raw in f ):
>                 b.append(columns[c])
> 
>         y = np.array(b, float)
>         print c, y
> 
> 
> I thought that  can get the arrays of the columns[5] to [10], but I only
> could get repetition of same arrays of columns[5].
> 
> The result was something like:
> 
> 5 [1 2 3 4 ......, 10 9 8]
> 6 [1 2 3 4 ......, 10 9 8]
> 7 [1 2 3 4 ......, 10 9 8]
> 8 [1 2 3 4 ......, 10 9 8]
> 9 [1 2 3 4 ......, 10 9 8]
> 10 [1 2 3 4 ......, 10 9 8]
> 
> 
> What I can't understand is that even though c increased incrementally upto 10,
> y arrays stay same.
> 
> Would someone help me to understand this problem more?

That's because the inner loop read from a file until his reaches
the end of the file.  Since you're not resetting the file pointer,
during the second and later runs of the outer loop, the inner loop
starts at the end of the file and terminates without any action.

You'd get more interesting results if you rewind the file:

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


Hope this helps,

-- HansM


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


#35858

FromIsaac Won <winefrog@gmail.com>
Date2012-12-31 14:11 -0800
Message-ID<d13e307e-7bb0-4b03-a139-fa3ec6d1afc9@googlegroups.com>
In reply to#35850
On Monday, December 31, 2012 6:59:34 AM UTC-6, Hans Mulder wrote:
> On 31/12/12 11:02:56, Isaac Won wrote:
> 
> > Hi all,
> 
> > I am a very novice for Python. Currently, I am trying to read continuous
> 
> > columns repeatedly in the form of array. 
> 
> > my code is like below:
> 
> > 
> 
> > import numpy as np
> 
> > 
> 
> > b = [] 
> 
> > c = 4
> 
> > f = open("text.file", "r")
> 
> > 
> 
> > while c < 10:
> 
> >         c = c + 1
> 
> > 
> 
> >         for  columns in ( raw.strip().split() for raw in f ):
> 
> >                 b.append(columns[c])
> 
> > 
> 
> >         y = np.array(b, float)
> 
> >         print c, y
> 
> > 
> 
> > 
> 
> > I thought that  can get the arrays of the columns[5] to [10], but I only
> 
> > could get repetition of same arrays of columns[5].
> 
> > 
> 
> > The result was something like:
> 
> > 
> 
> > 5 [1 2 3 4 ......, 10 9 8]
> 
> > 6 [1 2 3 4 ......, 10 9 8]
> 
> > 7 [1 2 3 4 ......, 10 9 8]
> 
> > 8 [1 2 3 4 ......, 10 9 8]
> 
> > 9 [1 2 3 4 ......, 10 9 8]
> 
> > 10 [1 2 3 4 ......, 10 9 8]
> 
> > 
> 
> > 
> 
> > What I can't understand is that even though c increased incrementally upto 10,
> 
> > y arrays stay same.
> 
> > 
> 
> > Would someone help me to understand this problem more?
> 
> 
> 
> That's because the inner loop read from a file until his reaches
> 
> the end of the file.  Since you're not resetting the file pointer,
> 
> during the second and later runs of the outer loop, the inner loop
> 
> starts at the end of the file and terminates without any action.
> 
> 
> 
> You'd get more interesting results if you rewind the file:
> 
> 
> 
> 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
> 
> 
> 
> 
> 
> Hope this helps,
> 
> 
> 
> -- HansM

Hi Hans,

I appreciate your advice and kind tips.

The both codes which you gave seem pretty interesting.

Both look working for incrementing inner loop number, but the results of y are added repeatedly such as [1,2,3],[1,2,3,4,5,6], [1,2,3,4,5,6,7,8,9]. Anyhow, really thank you for your help and I will look at this problem more in detail.

Isaac

[toc] | [prev] | [standalone]


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


csiph-web