0

我是编程新手,在这里找不到似乎正在回答这个确切问题的问答(尽管 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)
4

2 回答 2

0

如果我正确理解了您的问题,那么您可以跟踪所有匹配的组合,如下所示:

list1 = ['INTJ','ENTJ','ESTJ']
list2 = ['INTP', 'ESFJ', 'ISTJ']
import collections
import itertools
matches = collections.defaultdict(list)
for item1,item2 in itertools.product(list1,list2):
    match = ''
    for i in xrange(4):
        if item1[i] == item2[i]:
            match += item1[i]
    if len(match):
        matches[match].append((item1,item2))

matches最终为:

{'ITJ': [('INTJ', 'ISTJ')], 
 'EJ': [('ENTJ', 'ESFJ')], 
 'INT': [('INTJ', 'INTP')], 
 'J': [('INTJ', 'ESFJ')], 
 'STJ': [('ESTJ', 'ISTJ')], 
 'TJ': [('ENTJ', 'ISTJ')], 
 'T': [('ESTJ', 'INTP')], 
 'NT': [('ENTJ', 'INTP')], 
 'ESJ': [('ESTJ', 'ESFJ')]}

如果您只想计算给定匹配项的出现次数,您可以修改代码如下:

list1 = ['INTJ','ENTJ','ESTJ']
list2 = ['INTP', 'ESFJ', 'ISTJ']
import collections
import itertools
matches = collections.defaultdict(lambda: 0)
for item1,item2 in itertools.product(list1,list2):
    match = ''
    for i in xrange(4):
        if item1[i] == item2[i]:
            match += item1[i]
    if len(match):
        matches[match] += 1

这使:

{'ITJ': 1, 'EJ': 1, 'INT': 1, 'J': 1, 'STJ': 1, 'TJ': 1, 'T': 1, 'NT': 1, 'ESJ': 1}
于 2013-03-17T13:44:52.683 回答
0
import itertools as IT
import collections

list1 = ["INTJ","ENTJ","ESTJ"]
list2 = ["INTP","ESFJ","ISTJ"]

matches = collections.Counter()
for item1, item2 in zip(list1, list2):
    for n in range(1,4):
        for idx in IT.combinations(range(4), n):
            if all(item1[i] == item2[i] for i in idx):
                print(item1, item2, idx)
                matches[n] += 1

print(matches)

产量

% test.py
('INTJ', 'INTP', (0,))
('INTJ', 'INTP', (1,))
('INTJ', 'INTP', (2,))
('INTJ', 'INTP', (0, 1))
('INTJ', 'INTP', (0, 2))
('INTJ', 'INTP', (1, 2))
('INTJ', 'INTP', (0, 1, 2))
('ENTJ', 'ESFJ', (0,))
('ENTJ', 'ESFJ', (3,))
('ENTJ', 'ESFJ', (0, 3))
('ESTJ', 'ISTJ', (1,))
('ESTJ', 'ISTJ', (2,))
('ESTJ', 'ISTJ', (3,))
('ESTJ', 'ISTJ', (1, 2))
('ESTJ', 'ISTJ', (1, 3))
('ESTJ', 'ISTJ', (2, 3))
('ESTJ', 'ISTJ', (1, 2, 3))
Counter({1: 8, 2: 7, 3: 2})

我包含了一个额外的打印语句,以显示找到的匹配项。

('ESTJ', 'ISTJ', (2, 3))

表示这两个列表在索引 2 和 3 处匹配——即字母 T 和 J。

Counter({1: 8, 2: 7, 3: 2})

表示有 8 个 1 字母匹配、7 个 2 字母匹配和 2 个 3 字母匹配。

于 2013-03-17T13:45:24.170 回答