简体   繁体   English

比较匹配的字母并将其存储在两个单词列表中?

[英]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. 我是编程的新手,并且在Stackoverflow上阅读了一些有关在两个列表中匹配字符串的问答,但是找不到一个可以帮助我完成此确切任务的问题。

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: 我想遍历每个单词中的每个字母并存储所有进行的比较,列表中所有单词的匹配字母总数以及匹配的每个字母par,如下所示:

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. 我无法弄清楚如何在两个列表中的某个索引[i]处进行比较,因此我可以以某种方式将匹配项存储在变量中。 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. 使用zip来遍历1个以上的集合。 (note : this code assumes that two lists have same number of items, and every item is a correct intp profile) (注意:此代码假定两个列表具有相同数量的项目,并且每个项目都是正确的intp配置文件)

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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