CodyKochmann
7/22/2016 - 4:10 PM

calculate the similarity between two python lists

calculate the similarity between two python lists

def calculate_similarity(list_one, list_two):
    best_score=0.0
    if len(list_one)==len(list_two):
        for i in range(len(list_one)):
            if list_one[i]==list_two[i]:
                best_score+=1.0
    else:
        if len(list_one)<len(list_two):
            # switch the lists if one is bigger than two
            list_one+=list_two
            list_two=list_one[:len(list_one)-len(list_two)]
            list_one=list_one[len(list_two):]
        for x in range(len(list_one)-len(list_two)):
            is_similar=False
            similar = 0.0
            for i in range(len(list_two)):
                if list_one[i+x] == list_two[i]:
                    similar+=1.0
            if similar > best_score:
                best_score=similar
    return(best_score/float(len(list_one)))