0

So I have an assignment and here are the requirements,

For this program, we'll be generating more statistics about lists. The program will work with one list that the user inputs and the other is a set list of words. The program will count how many times each of the words from the set list exists in the input list, and display these results.

Some other things to note is that I cannot use other data structures such as Python's dictionary.

Here is a rough draft of what I have so far,

def main():
    setlist=['One', 'Two', 'Three']
    words=input('Input words')
    inputlist=getlist(words)
    print(inputlist)
    counts,word=comparelist(setlist,inputlist)
    print(counts)
    print(word)

def getlist(words):
    list1=[]
    count=0
    for i in words.split():
        j=[i,count]
        count+=1
        list1.append(j)
    return list1

def comparelist(setlist,inputlist):
    count=0
    for words in setlist:
        list2words=words
        if list2words in inputlist:
            count+=1
            return count, words
        else:
            count=count+0
            return count, words

main()

I'm still fairly new to Python, (about a few weeks practice) and I'm kinda stumped. I know it has to do with the third function but I seem to can't get it to work. Any help will be greatly appreciated.

Also, this is my first question I have asked on Stackoverflow, so any other recommendation with question etiquette would also be appreciated.

Thanks.

Ishmael
  • 11
  • 1
  • What has to do with the third function? – kylieCatt Aug 17 '15 at 12:22
  • 1
    I would recommend giving the outline of a test case you tried. So give an example input, the expected output for that input, and the actual output when you run it through your program :) – Michael S Priz Aug 17 '15 at 12:36
  • Can we use modules like [collections](https://docs.python.org/2/library/collections.html#collections.Counter)? – wenzul Aug 17 '15 at 12:48
  • 2
    possible duplicate of [How can I count the occurrences of a list item in Python?](http://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item-in-python) – wenzul Aug 17 '15 at 12:54

2 Answers2

1

I hope I understood you correctly (count how many times each of the words from the set list exists in the input list), but this might help:

# Two lists to compare:    
list1 = ['Words', 'in', 'list', 'one']
list2 = ['Words', 'in', 'list', 'two']

# Make a new list of all the common elements and take the length:
print len([i for i in list1 if i in list2])

>>> 3

Sorry if I misunderstood. If you want a shorthand way at least, then list comprehensions are useful. The following is probably what you wanted:

list1 = ['Words', 'list', 'oh', 'one']
list2 = ['Words', 'list', 'list', 'two']

print [list2.count(i) for i in list1]

>>> [1, 2, 0, 0]

An easy way to understand whats going on here; firstly for i in list1 loops of the items in list1. Then list2.count(i) counts the number of occurances of each of these items in list2. The [ ] means a new list is returned, hence the output in list format [1, 2, 0, 0]

kezzos
  • 3,023
  • 3
  • 20
  • 37
  • I believe OP wants a count of how many times _each_ word in the set list appears in the input list. – ILostMySpoon Aug 17 '15 at 12:44
  • I did not downvote you but I question how useful this will be to the OP. They are new to Python and have clearly stated that their question is an assignment question. I am not sure just giving the OP a pythonic answer is very helpful... – Michael S Priz Aug 17 '15 at 12:45
  • I see, I misunderstood. I don't think it is ever too soon to try and learn about list comprehensions, they are one of the best features of python after all :) – kezzos Aug 17 '15 at 12:52
0

Your error seems to be coming from a misunderstanding of what the return keyword does!

return passes a value back to the caller of the function and exits the function. That is, once you call return the rest of the function does not run!

Try fixing your code with this in mind and let me know if you have more trouble :)

Michael S Priz
  • 1,116
  • 7
  • 17