Is there anyway to compare lists or check to see how many of the objects in one list are the same as the other list?

eg:
L1 = [dog, cat, john, moo]
L2 = [dog, cow, moo, stuff]
there is two of the same "things" in it

I'm trying to write a program to decide what option would be best sort of thing.

thanks.
sorry bout spelling and uh my terminology


This is what i have so far but the very end is what i need help with.

# testing for list relativy thing
user_input = ["three", "four", "five", "six", "seven", "eight", "nine", "ten","one", "two"]
user_input1 = user_input
list1 = [["1",["one"]], # Each nest is one "thing"
         ["2",["two", "one"]],
         ["3",["one", "two", "three"]],
         ["4",["one", "two", "three", "four"]],
         ["5",["one", "two", "three", "four", "five"]],
         ["6",["one", "two", "three", "four", "five", "six"]],
         ["7",["one", "two", "three", "four", "five", "six", "seven"]],
         ["---------RELCHECK ERROR---------",["dog","horse"]],
         ["8",["one", "two", "three", "four", "five", "six", "seven", "eight"]],
         ["9",["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]],
         ["10",["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]] ]
relevant_facts =[]
a = 0
b = 0
c = 0
d = 0
e = 0
rellist = []
search_no = 0
facts_length = len(list1) # Checks how many things there are
input_length = len(user_input1) - 1
relcheck=0

# Find all relevant things
while relcheck < facts_length: # Make sure Its within the user_input range.
    keysize = len(list1[a][1]) - 1
    if list1[a][1][b] in user_input:
        rellist.append(list1[a])
        b = 0
        a += 1
        relcheck += 1
        print rellist
    else:
        if b < keysize:
            b +=1
        else:
            b = 0
            a += 1
            relcheck += 1
            

# Choose most relevant
# This is the hard bit for me
relsort = 0
rellistlen = len(rellist) - 1
while relsort < rellistlen:
    if rellist[c][1][d] in user_input

Thanks

After going away and coming back i figured out what I needed to do.
Mayswell show you the code.

# testing for list relativy thing
user_input = ["three", "four", "five", "six", "seven", "eight", "nine", "ten","one", "two"]
user_input1 = user_input
list1 = [["1",["one"]], # Each nest is one "thing"
         ["2",["two", "one"]],
         ["3",["one", "two", "three"]],
         ["4",["one", "two", "three", "four"]],
         ["5",["one", "two", "three", "four", "five"]],
         ["6",["one", "two", "three", "four", "five", "six"]],
         ["7",["one", "two", "three", "four", "five", "six", "seven"]],
         ["---------RELCHECK ERROR---------",["dog","horse"]],
         ["8",["one", "two", "three", "four", "five", "six", "seven", "eight"]],
         ["9",["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]],
         ["10",["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]] ]
relevant_facts =[]
a = 0
b = 0
c = 0
d = 0
e = 0
rellist = []
search_no = 0
facts_length = len(list1) # Checks how many things there are
input_length = len(user_input1) - 1
relcheck=0

# Find all relevant things
while relcheck < facts_length: # Make sure Its within the user_input range.
    keysize = len(list1[a][1]) - 1
    if list1[a][1][b] in user_input:
        rellist.append(list1[a])
        b = 0
        a += 1
        relcheck += 1
    else:
        if b < keysize:
            b +=1
        else:
            b = 0
            a += 1
            relcheck += 1
            
# This is the new bit I made.
# Choose most relevant
relsort = {}
rellistlen = len(rellist) - 1
while True:
    for item in user_input:
        keylen = len(rellist[c][1]) - 1
        d = d + rellist[c][1].count(item)
    relsort[d] = rellist[c][0]
    if c < rellistlen:
        d = 0
        c += 1
    else:
        break
relsort = sorted(relsort.items(), reverse=True)
relsort = relsort[0]
relsort = relsort[1]
relsort = int(relsort)
print relsort
print "done"

mayby that will be usefull to sombody somtime.
Bye.

I just skimmed your code, but it looks like a set() will do some of this for you. Using a builtin is easier and more reliable as it has been tested thousands of times. And cudos for figuring out the logic.

user_input = ["three", "four", "five", "six", "seven", "eight", "nine", "ten","one", "two"]
user_input1 = set(user_input)
list1 = set(["one", "two", "three", "four", "five", "six", "seven", "dog","horse"])

set_3 = list1.intersection(user_input1)
print "common to both", set_3
print "in user_input only", user_input1.difference(list1)
#
#Also, a dictionary would probably work better here
#
list1 = [["1",["one"]],
         ["2",["two", "one"]]]   ## snipped for testing
dic_1 = {"1":["one"],
         "2":["two", "one"] }
print "\n", dic_1["2"]
print dic_1["2"][1]

Oh, cool thanks man.
And its shorter.
By alot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.