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.

asked Aug 17, 2015 at 12:20
4
  • What has to do with the third function? Commented Aug 17, 2015 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 :) Commented Aug 17, 2015 at 12:36
  • Can we use modules like collections? Commented Aug 17, 2015 at 12:48
  • 2
    possible duplicate of How can I count the occurrences of a list item in Python? Commented Aug 17, 2015 at 12:54

2 Answers 2

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]

answered Aug 17, 2015 at 12:37
3
  • I believe OP wants a count of how many times each word in the set list appears in the input list. Commented Aug 17, 2015 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... Commented Aug 17, 2015 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 :) Commented Aug 17, 2015 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 :)

answered Aug 17, 2015 at 12:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.