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

2006-01-14 Thread John Joseph

--- Danny Yoo <[EMAIL PROTECTED]> wrote:

> >I get the error  for set
> >  >>> set((1,1,1,2,2,2,2,2,3,4,4,5))
> > Traceback (most recent call last):
> >   File "", line 1, in ?
> > NameError: name 'set' is not defined
> 
> Hi John,
> 
> For Python 2.3.4, you'll need to add the line:
> 
> ##
> from sets import Set as set
> ##
> 
> before trying to use 'set'.  Sets weren't so built
> into Python until
> 


Hi 
   Thanks to all , I was able to do the script for
removing the dupes from the list , I  had pasted my
script along with this mail 
  Thanks a Lot
Joseph John 

***

"""This program was to teach u 
   (a)How to remove the dupes from the list 
   (b) How to use try and except in case the
input is wrong 

a_biglist => is the list which we enter 
i_counter = > counter in for loop 
length  => length of the list / No of list
members  

   """ 

import sys
from sets import Set as set
a_biglist = []
i_counter = " "
length = " "

try:
length = int(raw_input("Enter the Length of the List
:   "))
except:
print "U should Have Enterd a  integer, Now Try again
"
#length = int(raw_input("Enter the Length of the List
:   "))
sys.exit()



for i_counter in  range(length):
try:
num = int(raw_input("Enter the Number for the list 
:  "))
a_biglist.append(num)
except:
print " U are not entering integer , please do enter
integer"
sys.exit()




def dupe_detector(sequence):
''' returns a list of sequence that are
duplicated'''
sequence = list(sequence)
seen_dupes = []
for item in set(sequence):
if sequence.count(item) > 1: 
seen_dupes.append(item) 
return seen_dupes   


def print_dupe_report(sequence):
'''prints a report of items in sequence that are
duplicated'''
dupes = dupe_detector(sequence)
dupes.sort()
for d in dupes:
print "%s was duplicated" %d


print "The List of Dupe No are "

print
""
print  dupe_detector(a_biglist)
print
""

print  "\t\t The Dupe record is \n \t \t It Prints
which all numbers got duplicated"

print_dupe_report(a_biglist)
print
""


print  "Now We should print only , once All the 
duplication number are removed "

print set(a_biglist)






___ 
Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voicemail 
http://uk.messenger.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-09 Thread Danny Yoo

> lot for the advice I got my concepts of def of functions , sort
> functions , count , cleared for me
>I was able to do and understand all the
>  function example , except �item_comparison� function
>I get the error  for set
>  >>> set((1,1,1,2,2,2,2,2,3,4,4,5))
> Traceback (most recent call last):
>   File "", line 1, in ?
> NameError: name 'set' is not defined

Hi John,

For Python 2.3.4, you'll need to add the line:

##
from sets import Set as set
##

before trying to use 'set'.  Sets weren't so built into Python until
Python 2.4, and so examples that use 'set()' will need that above line to
install the proper support.

Best of wishes!

___
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-09 Thread Kent Johnson
Brian van den Broek wrote:
> Your python is a version before set was a built-in type (2.4, I 
> believe). So, the iteration_comparison *and* dupe_detector_5 functions 
> won't work for you.
> 
> 2.3 did include the sets module, though.
> 
> So, stick
> 
> from sets import set
> 
> at the top of the code and it ought work.

The class name is different in 2.3 also. Use
from sets import Set as set

to get the same spelling as in 2.4.

Kent

___
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-09 Thread Brian van den Broek
John Joseph said unto the world upon 09/01/06 03:47 AM:
>  Hi  Brian 
> It was a  excellent  tutorial, Thanks a
> lot for the advice  I got my concepts of  def of
> functions , sort functions , count , cleared for me 
>I was able to do and understand all the
>  function example , except “item_comparison” function 
>I get the error  for set 
>  >>> set((1,1,1,2,2,2,2,2,3,4,4,5))
> Traceback (most recent call last):
>   File "", line 1, in ?
> NameError: name 'set' is not defined
> 
> my python versrion is 
> Python 2.3.4 (#1, Nov  4 2004, 14:06:56)
> [GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
> THANKS 
>   Joseph John 


Joseph,

glad you found the ramble of use :-)

Your python is a version before set was a built-in type (2.4, I 
believe). So, the iteration_comparison *and* dupe_detector_5 functions 
won't work for you.

2.3 did include the sets module, though.

So, stick

from sets import set

at the top of the code and it ought work.

Best,

Brian vdB
___
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-09 Thread John Joseph
 Hi  Brian 
It was a  excellent  tutorial, Thanks a
lot for the advice  I got my concepts of  def of
functions , sort functions , count , cleared for me 
   I was able to do and understand all the
 function example , except “item_comparison” function 
   I get the error  for set 
 >>> set((1,1,1,2,2,2,2,2,3,4,4,5))
Traceback (most recent call last):
  File "", line 1, in ?
NameError: name 'set' is not defined

my python versrion is 
Python 2.3.4 (#1, Nov  4 2004, 14:06:56)
[GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
THANKS 
  Joseph John 

--- Brian van den Broek <[EMAIL PROTECTED]> wrote:

> John Joseph said unto the world upon 08/01/06 06:36

We no longer need the continue clause, as converting
to set ensures we 
won't ever deal with the same item twice:

 >>> set((1,1,1,2,2,2,2,2,3,4,4,5))
set([1, 2, 3, 4, 5])


And, preventing us from dealing with the same item
twice is what makes 
this better. To see that, consider:

 >>> def iteration_comparison(sequence):
list_count = 0
set_count = 0
for i in list(sequence):
list_count += 1
for i in set(sequence):
set_count += 1
print list_count, set_count

=== message truncated ===






___ 
Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voicemail 
http://uk.messenger.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 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 "", 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 "", 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 "", 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 "", line 1, in -toplevel-
 dupe_detector_1(a_tuple)
   File "", 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(s

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 "", 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


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 "", 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


[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