背景
我有两个(列表的)列表,每个列表都是通过从两个地址表中读取数据创建的。
每行的第一个元素是列表行的唯一标识,其余元素用于地址比较。
每个列表看起来像这样:
列表 1(cli 添加)
['3', 'V8T5G2', 'VICTORIA', 'BR', 'CANADA']
['543', '234', '654', 'BELMONT', 'AVENUE', 'V8S3T4', 'VICTORIA', 'BR', 'CANADA']
['28', '70', 'RUSHTON', 'RD', 'M6C2X8', 'YK', 'ON', 'CANADA']
list 2(结构添加)
['7H0044', '234', '654', 'BELMONT', 'AVENUE', 'V8S3T4', 'VICTORIA', 'BC', 'CANADA']
['7H0033', 'V8T5G2', 'VICTORIA', 'BC', 'CANADA']
['7H0001', '700', 'RUSHTON', 'ROAD', 'M6C2X7', 'YORK', 'ON', 'CANADA']
['7H0034', '217', 'BONNYMUIR', 'DRIVE', 'V7S1L4', 'WEST', 'VANCOUVER', 'BC', 'CANADA']
任务目标 我的任务是将测试列表中的地址与另一个列表中的地址进行比较,并标记所有匹配和不匹配的记录。
我循环遍历列表 1 中的每一行,并与列表 2 的每一行进行逐元素比较。如果从列表 1 行中提取的所有元素都在列表 2 的任何行中找到,我将该记录标记为“匹配”并保留列表 2 中的行。已经能够识别完全匹配的记录。
问题点 真正的挑战是关于不匹配的行。对于列表 1 中的非匹配记录,我想从列表 2 中识别最匹配的行。例如如果列表 1 中的行在列表 2 中的三行中有匹配元素,我想选择列表 2 中匹配元素数量最多的行。
预期结果 在上面共享的数据中,列表 1 中的第二行 (id 542) 完全匹配。但是其他两个记录没有产生完全匹配。
我希望能够创建一个不匹配的记录列表并捕获不匹配的元素:
[[comparison record from list 1],[Most matching record from list 2],[non-matching elements from list 1]]
所以对于上面的共享数据,我需要一个新的(列表的)列表,看起来像这样:
[['3', 'V8T5G2', 'VICTORIA', 'BR', 'CANADA'],['7H0033', 'V8T5G2', 'VICTORIA', 'BC', 'CANADA'],['BR']]
[['28', '70', 'RUSHTON', 'RD', 'M6C2X8', 'YK', 'ON', 'CANADA'],['7H0001', '700', 'RUSHTON', 'ROAD', 'M6C2X7', 'YORK', 'ON', 'CANADA'],['70','RD','M6C2X8', 'YK']]
下面的代码得到了部分结果。我无法找到一种方法来过滤最匹配的行。
代码片段 这是我找到匹配和不匹配记录的方式(列表 1 由 cli_add_fnl 引用,列表 2 由 struc_add_fnl 引用)。还想出了列出不匹配元素和匹配元素计数的方法。只需要一种方法来只提取列表元素 1 的最大计数行。
### Step 4 - Identifying the matching and non matching addresses ###
validated_addresses_all = []
invalid_addresses_all = []
for cli_add in cli_add_fnl:
comparison_cli_add=cli_add.copy()
#removing the id column from comparison
comparison_cli_add.pop(0)
for struct_add in struct_add_fnl:
matching_elements = [address_element for address_element in comparison_cli_add if address_element in struct_add]
#capture the matching records
if matching_elements == comparison_cli_add:
validated_addresses_all.append(cli_add)
else:
invalid_addresses_all.append(cli_add)
invalid_addresses_all.append(struct_add)
invalid_addresses_all.append(len(set(comparison_cli_add) & set(struct_add)))
invalid_addresses_all.append(nonmatching_elements)
#remove the duplicate entries
fnl_validated_addresses = []
for add in validated_addresses_all:
if add not in fnl_validated_addresses:
fnl_validated_addresses.append(add)
请您参考如下方法:
这是一种忽略位置和第一项的方法,方法是比较 adds 和 struct_adds 中的值,并在内部保留最高匹配项的计数器.只要有匹配项,它就会更新计数器并获取最高匹配项的索引,在下面的示例中,它什么都不做。然后比较 add 中的项目和最高匹配项的差异。
然后将结果相应地附加到列表中。
adds = [['3', 'V8T5G2', 'VICTORIA', 'BR', 'CANADA'],
['543', '234', '654', 'BELMONT', 'AVENUE', 'V8S3T4', 'VICTORIA', 'BR', 'CANADA'],
['28', '70', 'RUSHTON', 'RD', 'M6C2X8', 'YK', 'ON', 'CANADA']]
struct_adds = [ ['7H0044', '234', '654', 'BELMONT', 'AVENUE', 'V8S3T4', 'VICTORIA', 'BC', 'CANADA'],
['7H0033', 'V8T5G2', 'VICTORIA', 'BC', 'CANADA'],
['7H0001', '700', 'RUSHTON', 'ROAD', 'M6C2X7', 'YORK', 'ON', 'CANADA'],
['7H0034', '217', 'BONNYMUIR', 'DRIVE', 'V7S1L4', 'WEST', 'VANCOUVER', 'BC', 'CANADA']]
results = []
for add in adds:
match_count = 0
match_index = 0
for idx,struct_add in enumerate(struct_adds):
matches = [add_item in struct_add[1:] for add_item in add[1:]]
if matches.count(True) > match_count:
match_count = matches.count(True)
match_index = idx
if match_count == 0:
pass # no matches
else:
highest_match = struct_adds[match_index]
differences = [i for i in add[1:] if i not in highest_match]
results.append([add,highest_match,differences])
或者如果你想使用 set 操作,这应该像评论中建议的那样更有效,你可以将 for block 替换为:
for add in adds:
match_count = 0
match_index = 0
for idx,struct_add in enumerate(struct_adds):
matches = set(add[1:]) & set(struct_add[1:])
if len(matches) > match_count:
match_count = len(matches)
match_index = idx
if match_count == 0:
pass # no matches
else:
highest_match = struct_adds[match_index]
differences = list(set(add[1:]) - set(highest_match[1:]))
results.append([add,highest_match,differences])
两者产生相同的结果:
results
>>
[[['3', 'V8T5G2', 'VICTORIA', 'BR', 'CANADA'],
['7H0033', 'V8T5G2', 'VICTORIA', 'BC', 'CANADA'],
['BR']],
[['543',
'234',
'654',
'BELMONT',
'AVENUE',
'V8S3T4',
'VICTORIA',
'BR',
'CANADA'],
['7H0044',
'234',
'654',
'BELMONT',
'AVENUE',
'V8S3T4',
'VICTORIA',
'BC',
'CANADA'],
['BR']],
[['28', '70', 'RUSHTON', 'RD', 'M6C2X8', 'YK', 'ON', 'CANADA'],
['7H0001', '700', 'RUSHTON', 'ROAD', 'M6C2X7', 'YORK', 'ON', 'CANADA'],
['70', 'RD', 'M6C2X8', 'YK']]]
我还应该补充一点,在此示例中并且为了不使事情进一步复杂化,它将采用第一 最高匹配。这部分在 if 子句中进行管理,比较真实匹配的计数必须大于当前匹配的计数。
