[Tutor] Testing if a number occurs more than once [dict version]

Scot Stevenson scot@possum.in-berlin.de
Tue Dec 3 17:16:02 2002


Uh, I may have screwed up here: 

===============================
def morethanone(L):
    numbersfound = {}
    for number in L:
        if number in numbersfound:
            return 1
        else:
            numbersfound[number]=1
    return 0
===============================

After rereading some stuff about dictionaries, I think the line

	if number in numbersfound:

probably should be

	if numbersfound.has_key(number):

after all: It seems that "in" does a linear search (just what we were trying 
to avoid) why "has_key" uses the hash table.

Which just goes to show: Newer isn't always better, a little knowledge is a 
dangerous thing, and it always darkest before the dawn. Or something like 
that...

Y, Scot


-- 
  Scot W. Stevenson -- scot@possum.in-berlin.de -- Zepernick, Germany