Python从两个变量中查找匹配/不匹配字符串

时间:2018-03-21 03:02:14

标签: python

需要从两个变量中识别匹配/不匹配的字符串。我只能根据相应的索引获取数据。我需要两个变量来检查所有数据并产生结果。

apdb = {'AP Database': [{'AP Type': '110H',
                  'Name': 'varagu'},
                 {'AP Type': '110H',
                  'Name': 'thinai'},
                 {'AP Type': '110H',
                  'Name': 'kambu'},
                 {'AP Type': '205H',
                  'Name': 'keppai'}]
        }

apdb1 = {'AP Database': [{'AP Type': '110H',
                  'Name': 'kudhiravali'},
                 {'AP Type': '110H',
                  'Name': 'kambu'},
                 {'AP Type': '110H',
                  'Name': 'solam'},
                 {'AP Type': '205H',
                  'Name': 'keppai'},
                 {'AP Type': '205H',
                  'Name': 'rice'}]
        }


ap_database = apdb.get('AP Database')
ap_database1 = apdb1.get('AP Database')


ap1 = []
ap2 = []

for ap_detail , ap_detail1 in zip(ap_database, ap_database1):
    str1= ap_detail.__getitem__('Name')
    str2= ap_detail1.__getitem__('Name')

    if str1 in str2:
        ap1.append((str1, 'Matched'))
    else:
        ap1.append((str1, 'Unmatched'))

    if str2 in str1:
        ap2.append((str2, 'Matched'))
    else:
        ap2.append((str2, 'Unmatched'))

print (ap1)
print (ap2)

它给出的结果在

之下
 [('varagu', 'Unmatched'), ('thinai', 'Unmatched'), ('kambu', 'Unmatched'), ('keppai', 'Matched')]
[('kudhiravali', 'Unmatched'), ('kambu', 'Unmatched'), ('solam', 'Unmatched'), ('keppai', 'Matched')]

实际上,“' Kambu'两个字符串都可以使用,但结果显示无与伦比,需要匹配,并且str2还有额外的单词' rice',结果中没有显示需要同时显示为&# 39;不可用'。

1 个答案:

答案 0 :(得分:2)

看看这个:

>>> apdb = {'AP Database': [{'AP Type': '110H',
                  'Name': 'varagu'},
                 {'AP Type': '110H',
                  'Name': 'thinai'},
                 {'AP Type': '110H',
                  'Name': 'kambu'},
                 {'AP Type': '205H',
                  'Name': 'keppai'}]
        }
>>> apdb1 = {'AP Database': [{'AP Type': '110H',
                  'Name': 'kudhiravali'},
                 {'AP Type': '110H',
                  'Name': 'kambu'},
                 {'AP Type': '110H',
                  'Name': 'solam'},
                 {'AP Type': '205H',
                  'Name': 'keppai'},
                 {'AP Type': '205H',
                  'Name': 'rice'}]
        }

# Extract the 'Name's and make 'set's
>>> ap_database = set([d['Name'] for d in apdb['AP Database']])
>>> ap_database1 = set([d['Name'] for d in apdb1['AP Database']])

# Intersection of two sets are the matched items
>>> ap_result = [(e, 'Matched') for e in ap_database.intersection(ap_database1)]
>>> ap_result1 = ap_result[:]

# Difference of two sets are umnatched
>>> ap_result += [(e,'Unmatched') for e in ap_database-ap_database1]
>>> ap_result1 += [(e,'Unmatched') for e in ap_database1-ap_database]

# Desired results
>>> ap_result
[('keppai', 'Matched'), ('kambu', 'Matched'), ('thinai', 'Unmatched'), ('varagu', 'Unmatched')]
>>> ap_result1
[('keppai', 'Matched'), ('kambu', 'Matched'), ('solam', 'Unmatched'), ('rice', 'Unmatched'), ('kudhiravali', 'Unmatched')]