简体   繁体   中英

Compare and store matching letters in two lists of words?

I'm totally new to programming and have read several Q & A here at Stackoverflow about matching strings in two lists, but don't find one that helps me with this exact task.

I have to lists, like this:

list1 = ["INTP", "ESFJ", "ENTJ"]
list2 = ["ENTP", "ESFP", "ISTJ"]

I want to iterate over each letter in each word and store all comparisons made, the total number of matching letters for all words in the lists and every par of letters that matches, like this:

total_letters_compared = 12
total_correct_matches = 8 
first_letter_pair_matches = 1
second_letter_pair_matches = 2
third_letter_pair_matches = 3
fourth_letter_pair_matches = 2

I can't figure out how to do the comparison at a certain index [i] in both lists so I can somehow store matches in my variables. What I´ve been able to come up with so far is the following:

total = 0
total_letters_compared = 0
total_correct_matches = 0
first_letter_pair_matches = 0
second_letter_pair_matches = 0
third_letter_pair_matches = 0
fourth_letter_pair_matches = 0

for combination in list2:
for letter in combination:
    total_letters_compared = total_letters_compared + 1
    if list2letter == list1.ltter:
        total_correct_matches = total_correct_matches + 1
        # here I´d like to keep track of which letter-pair is compared and
                    # add 1 to the correct variable or continue to the next letter-pair

use zip to iterate through more than 1 collections. (note : this code assumes that two lists have same number of items, and every item is a correct intp profile)

matches = {0:0, 1:0, 2:0, 3:0}

for item1, item2 in zip(list1, list2):
   for i in xrange(4):
      if item1[i]==item2[i]: 
         matches[i] += 1

and you can extract data you want by:

total_letters_compared = #length of a list * 4
total_correct_matches = #sum(matches.values())
nth_letter_pair_matches = #matchs[n-1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM