[Tutor] parsing an array
O.R.Senthil Kumaran
orsenthil at gmail.com
Sat Nov 10 04:08:49 CET 2007
* sith . <sith618 at yahoo.com> [2007-11-09 17:53:53]:
> newfile = open('y.txt')
> >>> for line in newfile:
> ... print line.rstrip()
>
> 3 5 7
> 11 8 10
This example is different from a array handling one. This is a file handle with with reading the contents of the file through a loop.
>
> Hi,
> I'm trying to parse an array. Now that I can open and read lines in my array, how can I access individual elements of my multidimensional array? I'd like to loop through the array and compare values in each line.
> If w is the array, if w[0][1]<w[0][2], write w[0][0] to w[0][3]
> Numpy? Thanks.
You dont require Numpy for a simple task as this.
Suppose your array is:
a = [[0,1,2,3,4,5],[1,2,3,4,5,6]]
You cannot modify the same array when you are looping through it. You have to loop through the copy of the contents :- a[:].
# Untested code
for i in a[:]: # You are looping through the copy of contents
for j in i:
# implement your logic with j
if j < i[0]: # or any dynamic conditional check.
a[i][j] = j
Does this help you?
Thanks,
--
O.R.Senthil Kumaran
http://uthcode.sarovar.org
More information about the Tutor
mailing list