TypeErrors

Scott David Daniels Scott.Daniels at Acm.Org
Sun Mar 1 18:03:33 EST 2009


Dennis Lee Bieber wrote:
> ...  Even this looks strange, since all list will contain is a copy of
> string for each occurrence of string in the values of shelve!
> Furthermore, the only use for the key is to obtain the value -- the key
> is never returned for later use... so...
> 
> 	def lookup(self, needle):
> 		cnt = 0
> 		for hay in self.shelve.values():
> 			if hay == needle:	cnt += 1
> 		return cnt
> 
> 	No need to go through keys() (presuming the documentation of shelve
> is correct, and all dictionary methods work), and the return value is an
> integer (if one really needs a list of duplicates, then 
> 
> 	return [needle] * cnt

OK, but you could do better:

     def lookup(self, needle):
         return self.shelve.values().count(needle)

or:
     def lookup(self, needle):
         return self.shelve.values().count(needle) * [needle]

depending on the needed result (until values starts returning an
iterator).

--Scott David Daniels
Scott.Daniels at Acm.Org




More information about the Python-list mailing list