Question about nested loop

I

Isaac Won

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
 
G

Gisle Vanem

Isaac Won said:
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
 
H

Hans Mulder

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
 
I

Isaac Won

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.
 
I

Isaac Won

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.
 
I

Isaac Won

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 = []
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top