计算两个列表中字母对匹配的组合?

时间:2013-03-17 13:21:11

标签: python string python-2.7 comparison

我是编程新手,无法找到Q&这里似乎回答了这个问题(虽然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)

2 个答案:

答案 0 :(得分: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}

答案 1 :(得分: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个字母的匹配项。