Hint: use the collections
module.
As for your code, some additional hints on style and other matters:
- Don't use the word
function
as the name for a function. function
is a "special" word and using it as a plain old function name shadows its special meaning.
- Don't use single-letter names (
i
) for loop variables** - use a descriptive name. Here for word in mylist:
would be appropriate.
- Your code has a logic error - if a
word
appears in the list, by definition word in list == True
. So counter
will never get past zero.
** Sidenote: single-letter variable names are bad style because they provide no information about what the variable means, or what it's supposed to contain. I personally only consider n
, m
, p
and i
, j
, k
to be acceptable loop variable names in mathematical code, and only then when used in the same way mathematicians use n,m,p i,j,k
. This is for historical reasons.
A hint towards finding your logic error:
# Relevant part of your code
my_list = ['a','b','c','d']
for item in my_list:
if item in my_list:
print "item %s in list" % item
else:
print "item %s not in list" % item
The output is:
item a in list
item b in list
item c in list
item d in list
This is because the code above is a tautology: You're taking a value from a list, and them immediately asking if that value occurs in that list. The answer is always going to be "yes".
This is not really the logical test you wanted. What you really want to do is keep track of the words you've already seen. Maybe you need some way of keeping track of which words you've already seen? Or possibly you just need a magical piece of code which will keep track of all the unique words you've seen? (Hint: look in the collections
module.)
Generally speaking, you would also be well served by learning how to use a debugger. This will let you see into the intermediate states of the program as it executes. Spyder
is a Python IDE with pdb
debugger integration (and a lot of other nice features.) Check it out.
Edit 4: You mention that you tried using the collections
module - good on you! - but that the output was unsuitable because you "need to return an int
".
Meditate on this:
>>> import collections
>>> my_string = "abc aabc ccab a acbbbaa"
>>> my_counter = collections.Counter(my_string)
>>> my_counter
Counter({'a': 8, 'b': 6, 'c': 5, ' ': 4})
>>> my_counter.keys() # Get a list of unique things in the counter
['a', ' ', 'c', 'b']
>>>
Do you know how to determine how many things are in a list?
Hint 2: You can see the attributes of an object by calling dir()
on it. If you don't know what you are allowed to do to an object, or what methods you can call on an object, do this to find out:
>>> dir(my_counter)
['__add__', '__and__', '__class__', '__cmp__', '__contains__', '__delattr__',
'__delitem__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__',
'__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__',
'__iter__', '__le__', '__len__', '__lt__', '__missing__', '__module__',
'__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__setitem__', '__sizeof__', '__str__', '__sub__',
'__subclasshook__', '__weakref__', 'clear', 'copy', 'elements', 'fromkeys',
'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys',
'most_common', 'pop', 'popitem', 'setdefault', 'subtract', 'update', 'values',
'viewitems', 'viewkeys', 'viewvalues']