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

John Joseph jjk_saji at yahoo.com
Sat Jan 14 14:46:43 CET 2006


--- Danny Yoo <dyoo at hkn.eecs.berkeley.edu> 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 "<stdin>", 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


More information about the Tutor mailing list