Home > Enterprise >  I dont even know what to write here
I dont even know what to write here

Time:08-22

Im making a script that compares the similarity between 2 names, and bassically i build one block of code to explain me. Every step that have the "####################" sign in the end, its because i dont know how to do it :( ¿Can anyone help me with this part of my project?

name_1 = ("Mike").lower() #"mike"
name_2 = ("Chris").lower() #"chris"

list_name_1 = [i for i in name_1] #["m", "i", "k", "e"]
list_name_2 = [i for i in name_2] #["c", "h", "r", "i", "s"]

list_of_both = list_name_1   list_name_2 #["m", "i", "k", "e", "c", "h", "r", "i", "s"]

#Python realizes that the letter "i" is repeated, so it saves it in this variable: ####################
repeated_letter = "i"

#Python recognizes the indexes where the repeated letter is: ####################
repeated_index1 = list_of_both[1] #["m", "THIS ONE", "k", "e", "c", "h", "r", "i", "s"] FIRST ONE
repeated_index2 = list_of_both[7] #["m", "i", "k", "e", "c", "h", "r", "THIS ONE", "s"] SECOND ONE

#Python deletes every index where the repeated letter is, but not the first one: ####################
list_without_repeated_letters = list_of_both - list_of_both[7] #["m", "i", "k", "e", "c", "h", "r", "s"]

#Python add 1 point to this variable "deleted_summary" for each missing letter:
deleted_summary = len(list_of_both - list_without_repeated_letters) #1

#Python set the value of the "repeated_letter_value" variable as "deleted_summary   1", because there is one index that has not been deleted:
repeated_letter_value = deleted_summary   1 #2

#Python assigns in the variable "points" 1 point for every letter in "list_without_repeated_letters", but the repeated letter "i" will have the repeated_letter_value as its value instead: ####################
points = [1, 2, 1, 1, 1, 1, 1, 1] #First step done

CodePudding user response:

I was a bit confused about what you want the return value to be but could you do something like this?

def compare_names(name1, name2):
    """
    Compares two names and returns a list of characters that have the same value in the same position.
    """
    name1 = name1.lower()
    name2 = name2.lower()

    # Initialize an empty list to store the characters that have the same value in the same position.
    same_value_same_position = []

    # Loop through the characters in name1.
    for i in range(len(name1)):
        # If the character in name1 is the same as the character in name2 at the same position, add it to the list.
        if name1[i] == name2[i]:
            same_value_same_position.append(name1[i])

    # Return the list of characters that have the same value in the same position.
    return same_value_same_position

CodePudding user response:

use collections.Counter

from collections import Counter

list_of_both = ["m", "i", "k", "e", "c", "h", "r", "i", "s"]
points = [v for k,v in Counter(list_of_both).items()]
print (points)

# [1, 2, 1, 1, 1, 1, 1, 1] 
  • Related