``````def who_knows(a, b):
""" a and b are lists """
c = []
for i in a:
if is_mem(b, i):
c.append(i)
return uniqify(c)

def is_mem(a, elem):
for i in a:
if i == elem:
return True
return False

def uniqify(arr):
b = {}
for i in arr:
b[i] = 1
return b.keys()``````
2
Contributors
1
2
Views
9 Years
Discussion Span
Last Post by JasonHippy
``````def who_knows(a, b):
""" a and b are lists """
c = []
for i in a:
if is_mem(b, i):
c.append(i)
return uniqify(c)

def is_mem(a, elem):
for i in a:
if i == elem:
return True
return False

def uniqify(arr):
b = {}
for i in arr:
b[i] = 1
return b.keys()``````

From what I can see, the who_knows() fuunction takes two lists as parameters.
It then iterates through list a and calls is_mem() passing list b and the current item

is_mem iterates through the passed in list and tries to determine whether or not the passed in item is in the list.
If the item is in the list, True is returned by is_mem().
If the item is not in the list, False is returned.

if is_mem returned true, then the current item in who_knows() is stored in list c

Once all of the items in list a have been iterated through, the who_knows() function passes list c into a function called uniquify() and returns the value that uniquify returns (a list!)

The uniquify function iterates through the passed in list and basically removes any duplicates.

To test the code, try the following:

``````# these are your functions
def who_knows(a, b):
""" a and b are lists """
c = []
for i in a:
if is_mem(b, i):
c.append(i)
return uniqify(c)

def is_mem(a, elem):
for i in a:
if i == elem:
return True
return False

def uniqify(arr):
b = {}
for i in arr:
b[i] = 1
return b.keys()

#create two lists...
list1 = ["1", "2", "3", "4", "4", "5", "6", "what?!", "who?", "what?!"]
list2 = ["2", "4", "6", "8", "10", "12", "what?!", "why?", "who?"]

# now we want to find out what things in list1 are in list2
# so we'll create a variable called something to store the
# value returned by our call to who_knows...
something = who_knows(list1, list2)

# Now let's see what we've got!
print something``````

Running that little script will yield the following result:

>>>

If we altered the last line of the who_knows function to:

``return c``

We'd get the following result from running the script:

>>>

The script you posted could equally be done with just one function..No need for the other two.
e.g.

``````# this is your code put into a single function...
def who_knows(listA, listB):
"""
Takes two lists as parameters and compares them
returns a list of items that are in both lists
"""
# list to store items in listA and listB
listC = []

# iterate through both lists
for a in listA:
for b in listB:
if a == b: # if a and b match, add item to listC
listC.append(a)

# remove any duplicates from the listC by storing indices in listD
listD = {}      # Note: curly braces {} instead of square ones [] !
for c in listC:
listD[c] = 1

# return listD
return listD.keys()

# little driver to test the function - same data as before
# create 2 lists
list1 = ["1", "2", "3", "4", "4", "5", "6", "what?!", "who?", "what?!"]
list2 = ["2", "4", "6", "8", "10", "12", "what?!", "why?", "who?"]

# create a variable to store the value returned by a call to who_knows
something = who_knows(list1, list2)

# print the result
print something``````

I hope that's cleared things up for you a bit!
Cheers for now,
Jas.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.