我是编程新手,在这里找不到似乎正在回答这个确切问题的问答(尽管 Python 上有很多,并且列表中的单词和字母匹配)。
我有两个这样的列表:
list1 = ["INTJ","ENTJ","ESTJ"]
list2 = ["INTP","ESFJ","ISTJ"]
共有 16 种可能的字母对组合。我已经设法计算出有多少完全匹配的字母对,例如 I/E(第一个)、S/N(第二个)、T/F(第三个)和 P/J(第四个)。
我如何跟踪有多少精确的 3 字母、2 字母和 1 字母匹配?以下是三字母匹配变体的示例:
ISTP and ISTJ (I + S + T)
INFP and INTP (I + N + P)
我从用户@thkang 那里得到了帮助,计算了这样的字母对匹配的数量,但我不知道如何重新排列这个代码来跟踪匹配的组合。我是否必须以其他方式存储匹配项才能完成此操作?
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
total_letters_compared = #length of a list * 4
total_correct_matches = #sum(matches.values())
nth_letter_pair_matches = #matchs[n-1]
这是我编写的代码,但我开始明白它只是普通的糟糕代码。有人可以帮我解决这个问题吗?
matches = {0:0, 1:0, 2:0, 3:0}
four_letter_matches = 0
three_letter_matches = 0
two_letter_matches = 0
for item1, item2 in zip(actual, typealyzer):
if item1[0:2] == item2[0:2]\
or item1[0], item1[1], item1[3] == item2[0], item2[1], item2[3]\
or item1[1], item1[2], item1[3] == item2[1], item2[2], item2[3]\
or item1[0], item1[2], item1[3] == item2[0], item2[2], item2[3]:
three_letter_matches = three_letter_matches + 1
elif item1[0:1] == item2[0:1]\
or item1[0], item1[1] == item2[0], item2[1]\
or item1[0], item1[2] == item2[0], item2[2]\
or item1[0], item1[3] == item2[0], item2[3]\
or item1[1], item1[2] == item2[1], item2[2]\
or item1[1], item1[3] == item2[1], item2[3]:
#and so forth...
two_letter_matches = two_letter_matches + 1
#I think I can get the one-letter matches by matches[1] or matches[2] or matches[3] or matches[4]
for i in xrange(4):
if item1[i]==item2[i]:
matches[i] += 1
我希望能够以某种方式分别打印出三个、两个和一个字母的匹配项
print str(three_letter_matches)
print str(two_letter_matches)
print str(one_letter_matches)