簡體   English   中英

比較匹配的字母並將其存儲在兩個單詞列表中?

[英]Compare and store matching letters in two lists of words?

我是編程的新手,並且在Stackoverflow上閱讀了一些有關在兩個列表中匹配字符串的問答,但是找不到一個可以幫助我完成此確切任務的問題。

我必須這樣列出:

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

我想遍歷每個單詞中的每個字母並存儲所有進行的比較,列表中所有單詞的匹配字母總數以及匹配的每個字母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]處進行比較,因此我可以以某種方式將匹配項存儲在變量中。 到目前為止,我能想到的是:

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

使用zip來遍歷1個以上的集合。 (注意:此代碼假定兩個列表具有相同數量的項目,並且每個項目都是正確的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