sith . wrote:
> a = [[1,1],[3,1.5],[5,0]]
> for i in range(len(a)) :
This should use range(1, len(a)). You don't want i to take on the value 0.
> if a[i][1] > a[i-1][1] :
When i==0 this compares a[0] to a[-1] which is the *last* element of the
list; a[0][1] > a[-1][1] so it prints 'greater
greater !!
greater !!
smaller
only the second and third "greater" and "smaller" output are correct as 1.5
is larger than 1 and 0 is smaller than 1.5.
What is i[1] greater than?
a = [[1,1],[3,1.5],[5,0]]
for i in range(len(a)) :
if a[i][1] > a[i-1][1] :
print 'greater !!'
else:
print "smaller"
greater !!
greater !!
smaller
Thanks for taking the time to help me Aditya. I tried the code but have
encountered a problem. In this new list,
[
On Nov 15, 2007 12:37 PM, sith . <[EMAIL PROTECTED]> wrote:
> a = [[1,2],[3,1.5],[5,6]]
> for i in a:
> print i
> if i[1]>i[0]:
> print "second index is larger"
> else:
> print "second index is smaller"
> [1, 2]
> second index is larger
> [3, 1.5]
> second index is smal
a = [[1,2],[3,1.5],[5,6]]
for i in a:
print i
if i[1]>i[0]:
print "second index is larger"
else:
print "second index is smaller"
[1, 2]
second index is larger
[3, 1.5]
second index is small
er
[5, 6]
second index is larger
What I'd like do is compare if 1.
On Nov 13, 2007 7:06 PM, bob gailer <[EMAIL PROTECTED]> wrote:
> Aditya Lal wrote:
> > [snip]
>
> > for i in a[:] will make i point to the elements of the list
> To be more precise:
> a[:] is a copy of the list
> the for statement assigns each list element in turn to i. Assign is not
> exactly the
Aditya Lal wrote:
> [snip]
> for i in a[:] will make i point to the elements of the list
To be more precise:
a[:] is a copy of the list
the for statement assigns each list element in turn to i. Assign is not
exactly the same as point.
___
Tutor maillis
On Nov 13, 2007 8:29 AM, sith . <[EMAIL PROTECTED]> wrote:
> 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
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
# I'm new I'm going to try and explain my und
* sith . <[EMAIL PROTECTED]> [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.
newfile = open('y.txt')
>>> for line in newfile:
... print line.rstrip()
3 5 7
11 8 10
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 comp
11 matches
Mail list logo