[Tutor] IndexError: list index out of range [ Program work fine , but gives this message , guidance requested ]

2006-01-08 Thread John Joseph
Hi All 
  I am trying to find out the duplicates in a list
, as for beginning I wanted to compare the  adjacent
elements in list , for this purpose I wrote a script
for checking the adjacent elements in a list,  if same
, it prints the result that , the adjacent numbers are
same , it not  it will print  adjacent numbers are
same 
 My program works ,  but at the end of
the program execution it throws error 

“Traceback (most recent call last):
  File arrayduplinital.py, line 27, in ?
if seats[j]  seats[j+1]:
IndexError: list index out of range”

 I tried my best to find the logic
, why this error is coming , I was not able to find
the reason ,  I request your guidance for the reason
for this error and how to avoid it 
 I am adding the code which I wrote for
this program

**

  find duplicates in a list
(1) Sort the array
(2) counter
  i = 0
  j = 0
(3) for j = length
if seats[j]  seats[j+1] == print
value j and j+1 are not same
   else  ===  print value j ,  j
+1 are same



seats = []
i = 0
length  = int(raw_input(Enter The Lenght of the array
:  ))

while i  length:
num = int(raw_input(Enter the Seats :  ))
seats.append(num)
i += 1
print seats

seats.sort()

j = 0
for j in range(length):
if seats[j]  seats[j+1]:
print  The Seat, j , And the seat,
j+1 , value, which are, seats[j], and, seats[j+1],
are not same 
else:
print The Seats ,j ,And the seats,
j+1, Values,, seats[j], and, seats[j+1], are
same
~





___ 
To help you stay safe and secure online, we've developed the all new Yahoo! 
Security Centre. http://uk.security.yahoo.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] IndexError: list index out of range [ Program work fine , but gives this message , guidance requested ]

2006-01-08 Thread Guillermo Fernandez Castellanos
Hi,

Look at this:
  i=[1,2,3]
  i[len(i)]
Traceback (most recent call last):
   File stdin, line 1, in ?
IndexError: list index out of range


This means that I have tried to access an element that is out of the 
list, in this case i[3], that is, the 4th element of the list.

You are doing the same. You do a
j in range(len(seats))
and then you are accessing your list with seats[j] and seats[j+1]. What 
happens when j = len(seats)-1 and you call seats[j+1]? :-)

It happens indeed the same that in the example I gave you first.

Hope it helps. Good luck,

G

John Joseph wrote:
 Hi All 
   I am trying to find out the duplicates in a list
 , as for beginning I wanted to compare the  adjacent
 elements in list , for this purpose I wrote a script
 for checking the adjacent elements in a list,  if same
 , it prints the result that , the adjacent numbers are
 same , it not  it will print  adjacent numbers are
 same 
  My program works ,  but at the end of
 the program execution it throws error 
 
 “Traceback (most recent call last):
   File arrayduplinital.py, line 27, in ?
 if seats[j]  seats[j+1]:
 IndexError: list index out of range”
 
  I tried my best to find the logic
 , why this error is coming , I was not able to find
 the reason ,  I request your guidance for the reason
 for this error and how to avoid it 
  I am adding the code which I wrote for
 this program
 
 **
 
   find duplicates in a list
 (1) Sort the array
 (2) counter
   i = 0
   j = 0
 (3) for j = length
 if seats[j]  seats[j+1] == print
 value j and j+1 are not same
else  ===  print value j ,  j
 +1 are same
 
 
 
 seats = []
 i = 0
 length  = int(raw_input(Enter The Lenght of the array
 :  ))
 
 while i  length:
 num = int(raw_input(Enter the Seats :  ))
 seats.append(num)
 i += 1
 print seats
 
 seats.sort()
 
 j = 0
 for j in range(length):
 if seats[j]  seats[j+1]:
 print  The Seat, j , And the seat,
 j+1 , value, which are, seats[j], and, seats[j+1],
 are not same 
 else:
 print The Seats ,j ,And the seats,
 j+1, Values,, seats[j], and, seats[j+1], are
 same
 ~
 
 
 
 
   
 ___ 
 To help you stay safe and secure online, we've developed the all new Yahoo! 
 Security Centre. http://uk.security.yahoo.com
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] IndexError: list index out of range [ Program work fine , but gives this message , guidance requested ]

2006-01-08 Thread John Joseph

--- Guillermo Fernandez Castellanos
[EMAIL PROTECTED] wrote:

 Hi,
 
 Look at this:
   i=[1,2,3]
   i[len(i)]
 Traceback (most recent call last):
File stdin, line 1, in ?
 IndexError: list index out of range
 
 
 This means that I have tried to access an element
 that is out of the 
 list, in this case i[3], that is, the 4th element of
 the list.
 
 You are doing the same. You do a
 j in range(len(seats))
 and then you are accessing your list with seats[j]
 and seats[j+1]. What 
 happens when j = len(seats)-1 and you call
 seats[j+1]? :-)
 
 It happens indeed the same that in the example I
 gave you first.
 
 Hope it helps. Good luck,


Thanks for the advice  
 But I need to display the results 
  What should I do in the for loop  for this message
to go 
  my for loop is as 

for   j in range(length)  :
if seats[j]  seats[j+1]:
print  The Seat, j , And the seat,
j+1 , value, which are, seats[j], and, seats[j+1],
are not same 
else:
print The Seats ,j ,And the seats,
j+1, Values,, seats[j], and, seats[j+1], are
same
  Thanks 
 Joseph John 

 



___ 
Yahoo! Exclusive Xmas Game, help Santa with his celebrity party - 
http://santas-christmas-party.yahoo.net/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Landscape Printing

2006-01-08 Thread John Corry
Hi,

My text file is printing out in portrait.  Is there any instruction that I
can use so that notepad prints it in landscape?

Thanks,

John.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] IndexError: list index out of range [ Program work fine , but gives this message , guidance requested ]

2006-01-08 Thread Brian van den Broek
John Joseph said unto the world upon 08/01/06 06:36 AM:
 --- Guillermo Fernandez Castellanos
 [EMAIL PROTECTED] wrote:
 
 
Hi,

Look at this:
  i=[1,2,3]
  i[len(i)]
Traceback (most recent call last):
   File stdin, line 1, in ?
IndexError: list index out of range


This means that I have tried to access an element
that is out of the 
list, in this case i[3], that is, the 4th element of
the list.

You are doing the same. You do a
j in range(len(seats))
and then you are accessing your list with seats[j]
and seats[j+1]. What 
happens when j = len(seats)-1 and you call
seats[j+1]? :-)

It happens indeed the same that in the example I
gave you first.

Hope it helps. Good luck,

 
 
 Thanks for the advice  
  But I need to display the results 
   What should I do in the for loop  for this message
 to go 
   my for loop is as 
 
 for   j in range(length)  :
 if seats[j]  seats[j+1]:
 print  The Seat, j , And the seat,
 j+1 , value, which are, seats[j], and, seats[j+1],
 are not same 
 else:
 print The Seats ,j ,And the seats,
 j+1, Values,, seats[j], and, seats[j+1], are
 same
   Thanks 
  Joseph John 


Joseph,

I think Guillermo has already given you the answer :-).  But, I'll 
make it more explicit.

I'll also do a rather more. My intent is to give you some idea of how 
you can go about incrementally improving your code.

The code you posted is doing something like:

  a_list = [7,8,7]
  for index in range(len(a_list)):
print index, a_list[index]
print index + 1, len(a_list), a_list[index + 1]


0 7
1 3 8
1 8
2 3 7
2 7
3 3

Traceback (most recent call last):
   File pyshell#97, line 3, in -toplevel-
 print index + 1, len(a_list), a_list[index + 1]
IndexError: list index out of range

Examine the output. When the iteration hits the last member of the 
list (when it gets to the final element of range(len(a_list)), there 
is no next element of the list, so the request to print the next 
element doesn't work. The list length is 3, so I get the same 
exception as if I'd done:

  a_list[3]

Traceback (most recent call last):
   File pyshell#16, line 1, in -toplevel-
 a_list[3]
IndexError: list index out of range

directly. That suggests *not* asking for an iteration which uses up 
the list -- leave a next element and the code will work:

  for index in range(len(a_list) - 1):  # Note the difference
print index, a_list[index]
print index + 1, len(a_list), a_list[index + 1]


0 7
1 3 8
1 8
2 3 7
 


Of course, as you pointed out in your OP, right now, even with this 
fix, you will only be testing for sequential duplicates. My test list

a_list = [7,8,7]

has dupes, but your approach won't find them.

Warning: I'm going to go into a number of issues that you might not 
yet entirely understand. If I lose you, don't worry; just ask about it.


Let's both fix the requirement that the duplicates be sequential and 
start putting the code into functions.

Consider this:

  def dupe_detector_1(sequence):
for item in sequence:
if sequence.count(item)  1:
print %s is duplicated! %item


  dupe_detector_1(a_list)
7 is duplicated!
7 is duplicated!
 

OK, that might be better, as we can find non-sequential duplicates. 
But, there are two problems. 1) The duplication warning is duplicated 
:-) and 2)

  a_tuple = (4,3,4)
  dupe_detector_1(a_tuple)

Traceback (most recent call last):
   File pyshell#47, line 1, in -toplevel-
 dupe_detector_1(a_tuple)
   File pyshell#28, line 3, in dupe_detector_1
 if sequence.count(item)  1:
AttributeError: 'tuple' object has no attribute 'count'
 

The function signature seems to indicate it will work for all 
sequences, but it chokes on tuples.

The second problem is easy to fix:

  def dupe_detector_2(sequence):
# coerce to a list as lists have count methods.
sequence = list(sequence)
for item in sequence:
if sequence.count(item)  1:
print %s is duplicated! %item


  dupe_detector_2((1,1,2))
1 is duplicated!
1 is duplicated!
 

1 down, 1 to go.

  def dupe_detector_3(sequence):
sequence = list(sequence)
seen_dupes = []
for item in sequence:
if item in seen_dupes:
# if it is there, we already know it is duplicated
continue
elif sequence.count(item)  1:
print %s is duplicated! %item
seen_dupes.append(item)


  dupe_detector_3(a_list)
7 is duplicated!
 

That's much better :-)


There remain 2 things I'd want to do differently.

First, I'd separate the print logic from the detection logic:

  def dupe_detector_4(sequence):
'''returns a list of items in sequence that are duplicated'''
sequence = list(sequence)
seen_dupes = 

[Tutor] Python on Windows: any way to access shortcut's info?

2006-01-08 Thread Terry Carroll
I would like to know the name of the shortcut (link, I think, in Unix
parlance) from which a python program is being invoked.  I'd also like to
be able to access the directory where the shortcut lives.

Toy example: here's a directory structure:

 C:\
   test\
 argtest\
   arga.py
   argb.py
   testdir\
  argc.py

arga.py contains:

  import sys,os
  print ARGS:, sys.argv
  print WD:, os.getcwd()
  raw_input()  # just to keep the window open

argb.py and argc.py are both shortcuts to arga.py

When I run arga.py (e.g., by double-clicking on it, I get, as expected:

ARGS: ['C:\\test\\argtest\\arga.py']
WD: C:\test\argtest


When I run argb.py, I get the same thing.  I'd hoped (but didn't really
expect) sys.argv would be ['C:\\test\\argtest\\argb.py'] .

When I run argc.py, same thing.  In this case, I'd hoped sys.argv would be 
['C:\\test\\argtest\\testdir\\argc.py']; and os.getcwd() would have given 
back 'C:\test\argtest\testdir'.

Why do I care?

Well, I'd like to set up a directory where the shortcut and all its 
files will live.  I'd hoped keying off of the shortcut name might be an 
easy way to do this.

I suspect I'm out of luck: my guess is that windows does all the lookup of 
what the shortcut points to, and then launches the file pointed to; and by 
the time Python gets control, the shortcut information is long gone.  
Anyone know for sure?

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python on Windows: any way to access shortcut's info?

2006-01-08 Thread Kent Johnson
Terry Carroll wrote:
 I would like to know the name of the shortcut (link, I think, in Unix
 parlance) from which a python program is being invoked.  I'd also like to
 be able to access the directory where the shortcut lives.

You can set the command line arguments and working directory for a 
shortcut by opening the properties window on the shortcut, at least in 
Win2k.

So for example in the properties for argc.py just add 
'C:\\test\\argtest\\testdir\\argc.py to the Target field and set the 
Start in field to 'C:\\test\\argtest\\testdir'

If arga.py imports any packages in argtest\ you will have to add that 
dir to sys.path somehow (many options for this).

Kent
 
 Toy example: here's a directory structure:
 
  C:\
test\
  argtest\
arga.py
argb.py
testdir\
   argc.py
 
 arga.py contains:
 
   import sys,os
   print ARGS:, sys.argv
   print WD:, os.getcwd()
   raw_input()  # just to keep the window open
 
 argb.py and argc.py are both shortcuts to arga.py
 
 When I run arga.py (e.g., by double-clicking on it, I get, as expected:
 
 ARGS: ['C:\\test\\argtest\\arga.py']
 WD: C:\test\argtest
 
 
 When I run argb.py, I get the same thing.  I'd hoped (but didn't really
 expect) sys.argv would be ['C:\\test\\argtest\\argb.py'] .
 
 When I run argc.py, same thing.  In this case, I'd hoped sys.argv would be 
 ['C:\\test\\argtest\\testdir\\argc.py']; and os.getcwd() would have given 
 back 'C:\test\argtest\testdir'.
 
 Why do I care?
 
 Well, I'd like to set up a directory where the shortcut and all its 
 files will live.  I'd hoped keying off of the shortcut name might be an 
 easy way to do this.
 
 I suspect I'm out of luck: my guess is that windows does all the lookup of 
 what the shortcut points to, and then launches the file pointed to; and by 
 the time Python gets control, the shortcut information is long gone.  
 Anyone know for sure?
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor