Python 在两个列表中计算字母对匹配的组合?
我是编程新手,在这里找不到一个似乎能回答这个确切问题的问答(尽管有很多关于Python的文章和列表中匹配的单词和字母) 我有两个类似的列表:Python 在两个列表中计算字母对匹配的组合?,python,string,python-2.7,comparison,Python,String,Python 2.7,Comparison,我是编程新手,在这里找不到一个似乎能回答这个确切问题的问答(尽管有很多关于Python的文章和列表中匹配的单词和字母) 我有两个类似的列表: list1 = ["INTJ","ENTJ","ESTJ"] list2 = ["INTP","ESFJ","ISTJ"] 总共有16种可能的字母对组合。我已经计算了字母对的精确匹配数量,比如I/E(第一)、S/N(第二)、T/F(第三)和P/J(第四) 我如何记录有多少个精确的3字母、2字母和1字母匹配?以下是三个字母匹配的变体示例: IST
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)
我已经从user@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)
如果我正确理解了您的问题,那么您可以按如下方式跟踪所有匹配的组合:
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))
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': [('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(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))
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}
屈服
% 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})
表示有八个1字母匹配、七个2字母匹配和两个3字母匹配。jung/briggs-myers个性测试。。。MBTI;)您能否为上面的示例显示所需的输出?“字母对”的16种组合是什么?什么构成两个字母的匹配?@isedev-正确。:-)谢谢,我的问题还不够清楚-你回答的结果比我现在想要达到的更具体,但它肯定会帮助我在学习的道路上走得更远!我真的很想把每3、2个字母组合的点击数加起来。