简体   繁体   中英

Comparing two lists in Python

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.

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]

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM