I have a dictionary containing a list of similar persons. So from the above declarations, I want David and charles to be returned as one list of similar persons,based on common interest(s) (in this case data mining) and Ramesh and Suresh as a second list of similar persons (genetics common in both). How to accomplish this (result without a function is fine)?
dataset={
'David':['Artificial Intelligence','Machine learning', 'Neural networks', 'data mining'],
'Charles':['embedded computing','data mining','digital filters','signal processing','virtual reality','augmented reality'],
'Ramesh':['molecular biology','genetics','neuro surgery','oncology','ophthalmology'],
'Suresh':['genetics','neurology','ENT','bioinformatics','gene processing','radiology','pharmacology']
}
def commoninterest(personi,personj):
similar_persons=[]
for interest in dataset[personi]:
if interest in dataset[personj]:
similar_persons.append(personi,personj)
return similar_persons
2
Answers
The problem has not defined exactly. The example suggest that one common attribute is enough for making two persons similar. In this case you should create as many list of persons as many topics you have. (Perhaps, you can eliminate the empty lists.)
I you would like to make a more sophisticated measure you should define a metric between persons based on the number of common interests. In that case I advice to use sets for interests instead of lists, because
Like Imre Piller said, you want to store interests in lists. Here is one possible sollution. In addition, the function tells you what interests the pairs have in common, but you can get rid of that if you want.
result (python 2):