0

私はプログラミングにまったく慣れていないので、Stackoverflow で 2 つのリストの文字列の一致に関する Q & A をいくつか読みましたが、この正確なタスクに役立つものは見つかりませんでした。

次のようにリストする必要があります。

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

次のように、各単語の各文字を反復処理し、行われたすべての比較、リスト内のすべての単語の一致する文字の総数、および一致する文字のすべての部分を保存します。

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
4

1 に答える 1

1

zipを使用して、複数のコレクションを反復処理します。(注:このコードは、2つのリストに同じ数のアイテムがあり、すべてのアイテムが正しい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]
于 2013-03-16T21:43:43.047 に答える