skip to Main Content

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


  1. 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

    • it guarantees that the elements remain unique,
    • the order of attributes does not matter (as I see from the example),
    • you can use intersect set operation for calculating the common attributes, and
    • it makes your code faster.
    Login or Signup to reply.
  2. 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.

    dataset={
    'David':set(['Artificial Intelligence','Machine learning', 'Neural networks', 'data mining']),
    'Charles':set(['embedded computing','data mining','digital filters','signal processing','virtual reality','augmented reality']),
    'Ramesh':set(['molecular biology','genetics','neuro surgery','oncology','ophthalmology']),
    'Suresh':set(['genetics','neurology','ENT','bioinformatics','gene processing','radiology','pharmacology'])
    }
    
    def get_common_intrests(people):
        pairs = []
        p_list = list(people)
        for i, p1 in enumerate(p_list):
            for p2 in p_list[:i]:
                common_interests = people[p1].intersection(people[p2])
                if len(common_interests) > 0:
                    pairs.append([p1, p2, common_interests])
        return pairs
    
    print get_common_intrests(dataset)
    

    result (python 2):

    [['Suresh', 'Ramesh', set(['genetics'])], ['David', 'Charles', set(['data mining'])]]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search