Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 合并具有少量结构的两个可能不相等的列表_Python_Algorithm_Python 3.x_List_Merge - Fatal编程技术网

Python 合并具有少量结构的两个可能不相等的列表

Python 合并具有少量结构的两个可能不相等的列表,python,algorithm,python-3.x,list,merge,Python,Algorithm,Python 3.x,List,Merge,我正在努力解决一个相当困难的问题。 我想计算两个列表之间的某种差异度量。一个是经过验证的正确数据,另一个是由程序生成的。 我想测试这个程序有多精确,但为了实现这一点,我需要以某种方式合并这两个列表 下面可以找到示例数据 预期输出:字典列表,每个条目指定是否有两个匹配条目。如果它们不匹配,则应该有一个错误类型,指定错误 输出列表的长度必须是两个列表中最大的一个列表的长度,并且此列表中的每个条目都应该是以下其中之一: {匹配:True} {匹配:False,错误\类型:已删除} {匹配:False,

我正在努力解决一个相当困难的问题。 我想计算两个列表之间的某种差异度量。一个是经过验证的正确数据,另一个是由程序生成的。 我想测试这个程序有多精确,但为了实现这一点,我需要以某种方式合并这两个列表

下面可以找到示例数据

预期输出:字典列表,每个条目指定是否有两个匹配条目。如果它们不匹配,则应该有一个错误类型,指定错误

输出列表的长度必须是两个列表中最大的一个列表的长度,并且此列表中的每个条目都应该是以下其中之一:

{匹配:True} {匹配:False,错误\类型:已删除} {匹配:False,错误\u类型:插入} {匹配:False,错误\u类型:更新} 我在Python3中工作,所以如果有人能够提供Python代码,那将是令人惊讶的,但是一些精确描述这种算法的伪代码是非常有帮助的! 如果您找到了一种更简单的表示输出数据的方法,也可以

到目前为止,我掌握的并不多,我真的不知道该如何开始,但以下是我目前掌握的少量代码:

compare_data = [{} for i in range(max(len(correct_data), len(program_data)))]
for i in range(len(compare_data)):
    if len(program_data) <= i:
        compare_data[i]['matching'] = False
        compare_data[i]['error_type'] = 'deleted'
    elif len(correct_data) <= i:
        compare_data[i]['matching'] = False
        compare_data[i]['error_type'] = 'inserted'
    elif correct_data[i]['type'] != program_data[i]['type']:
        compare_data[i]['matching'] = False
        compare_data[i]['matching'] = 'updated'
    else:
        compare_data[i]['matching'] = True
    # I really don't have a clue what to do...
该程序可以生成各种错误数据,以下是一些示例:

缺少条目:

[
    {"type": "a", "data":["some", "random"]},
    {"type": "b", "data":["data", "for", "people", "to"]},
    {"type": "b", "data":["mess", "with", "on", "stack"]},
    {"type": "c", "data":["can", "try", "and"]},
    {"type": "d", "data":["help", "me"]}
]
预期产出:

[
    {"matching": True},
    {"matching": True},
    {"matching": True},
    {"matching": False, "error_type": "delete"},
    {"matching": True},
    {"matching": True},
]
[
    {"matching": True},
    {"matching": False, "error_type": "updated"},
    {"matching": False, "error_type": "updated"},
    {"matching": True},
    {"matching": True},
    {"matching": True},
]
[
    {"matching": False, "error_type": "deleted"},
    {"matching": True},
    {"matching": True},
    {"matching": False, "error_type": "deleted"},
    {"matching": True},
    {"matching": True},
]
[
    {"matching": True},
    {"matching": False, "error_type": "deleted"},
    {"matching": True},
    {"matching": False, "error_type": "deleted"},
    {"matching": True},
    {"matching": True},
]
[
    {"matching": True},
    {"matching": True},
    {"matching": False, "error_type": "updated"},
    {"matching": False, "error_type": "inserted"},
    {"matching": True},
    {"matching": True},
    {"matching": True}
]
交换的数据:

[
    {"type": "a", "data":["some", "random"]},
    {"type": "b", "data":["mess", "for", "people", "stack"]},
    {"type": "b", "data":["data", "with", "on", "to"]},
    {"type": "b", "data":["over", "flow", "so", "they"]},
    {"type": "c", "data":["can", "try", "and"]},
    {"type": "d", "data":["help", "me"]}
]
预期产出:

[
    {"matching": True},
    {"matching": True},
    {"matching": True},
    {"matching": False, "error_type": "delete"},
    {"matching": True},
    {"matching": True},
]
[
    {"matching": True},
    {"matching": False, "error_type": "updated"},
    {"matching": False, "error_type": "updated"},
    {"matching": True},
    {"matching": True},
    {"matching": True},
]
[
    {"matching": False, "error_type": "deleted"},
    {"matching": True},
    {"matching": True},
    {"matching": False, "error_type": "deleted"},
    {"matching": True},
    {"matching": True},
]
[
    {"matching": True},
    {"matching": False, "error_type": "deleted"},
    {"matching": True},
    {"matching": False, "error_type": "deleted"},
    {"matching": True},
    {"matching": True},
]
[
    {"matching": True},
    {"matching": True},
    {"matching": False, "error_type": "updated"},
    {"matching": False, "error_type": "inserted"},
    {"matching": True},
    {"matching": True},
    {"matching": True}
]
多个不同类型的缺失条目:

[
    {"type": "b", "data":["data", "for", "people", "to"]},
    {"type": "b", "data":["mess", "with", "on", "stack"]},
    {"type": "c", "data":["can", "try", "and"]},
    {"type": "d", "data":["help", "me"]}
]
预期产出:

[
    {"matching": True},
    {"matching": True},
    {"matching": True},
    {"matching": False, "error_type": "delete"},
    {"matching": True},
    {"matching": True},
]
[
    {"matching": True},
    {"matching": False, "error_type": "updated"},
    {"matching": False, "error_type": "updated"},
    {"matching": True},
    {"matching": True},
    {"matching": True},
]
[
    {"matching": False, "error_type": "deleted"},
    {"matching": True},
    {"matching": True},
    {"matching": False, "error_type": "deleted"},
    {"matching": True},
    {"matching": True},
]
[
    {"matching": True},
    {"matching": False, "error_type": "deleted"},
    {"matching": True},
    {"matching": False, "error_type": "deleted"},
    {"matching": True},
    {"matching": True},
]
[
    {"matching": True},
    {"matching": True},
    {"matching": False, "error_type": "updated"},
    {"matching": False, "error_type": "inserted"},
    {"matching": True},
    {"matching": True},
    {"matching": True}
]
具有相同类型的多个缺失条目:

[
    {"type": "a", "data":["some", "random"]},
    {"type": "b", "data":["mess", "with", "on", "stack"]},
    {"type": "c", "data":["can", "try", "and"]},
    {"type": "d", "data":["help", "me"]}
]
预期产出:

[
    {"matching": True},
    {"matching": True},
    {"matching": True},
    {"matching": False, "error_type": "delete"},
    {"matching": True},
    {"matching": True},
]
[
    {"matching": True},
    {"matching": False, "error_type": "updated"},
    {"matching": False, "error_type": "updated"},
    {"matching": True},
    {"matching": True},
    {"matching": True},
]
[
    {"matching": False, "error_type": "deleted"},
    {"matching": True},
    {"matching": True},
    {"matching": False, "error_type": "deleted"},
    {"matching": True},
    {"matching": True},
]
[
    {"matching": True},
    {"matching": False, "error_type": "deleted"},
    {"matching": True},
    {"matching": False, "error_type": "deleted"},
    {"matching": True},
    {"matching": True},
]
[
    {"matching": True},
    {"matching": True},
    {"matching": False, "error_type": "updated"},
    {"matching": False, "error_type": "inserted"},
    {"matching": True},
    {"matching": True},
    {"matching": True}
]
添加条目也可以添加多个条目当然:

[
    {"type": "a", "data":["some", "random"]},
    {"type": "b", "data":["data", "for", "people", "to"]},
    {"type": "b", "data":["oops", "with", "got", "mangled"]},
    {"type": "b", "data":["mess", "these", "on", "stack"]},
    {"type": "b", "data":["over", "flow", "so", "they"]},
    {"type": "c", "data":["can", "try", "and"]},
    {"type": "d", "data":["help", "me"]}
]
预期产出:

[
    {"matching": True},
    {"matching": True},
    {"matching": True},
    {"matching": False, "error_type": "delete"},
    {"matching": True},
    {"matching": True},
]
[
    {"matching": True},
    {"matching": False, "error_type": "updated"},
    {"matching": False, "error_type": "updated"},
    {"matching": True},
    {"matching": True},
    {"matching": True},
]
[
    {"matching": False, "error_type": "deleted"},
    {"matching": True},
    {"matching": True},
    {"matching": False, "error_type": "deleted"},
    {"matching": True},
    {"matching": True},
]
[
    {"matching": True},
    {"matching": False, "error_type": "deleted"},
    {"matching": True},
    {"matching": False, "error_type": "deleted"},
    {"matching": True},
    {"matching": True},
]
[
    {"matching": True},
    {"matching": True},
    {"matching": False, "error_type": "updated"},
    {"matching": False, "error_type": "inserted"},
    {"matching": True},
    {"matching": True},
    {"matching": True}
]

你好像在扩散。差分并不是那么容易做到的

Python有difflib和一些内置的diff算法。如果您对结果满意,那么您可以使用下面概述的这类东西:

from difflib import SequenceMatcher

correct = [
    {"type": "a", "data": ["some", "random"]},
    {"type": "b", "data": ["data", "for", "people", "to"]},
    {"type": "b", "data": ["mess", "with", "on", "stack"]},
    {"type": "b", "data": ["over", "flow", "so", "they"]},
    {"type": "c", "data": ["can", "try", "and"]},
    {"type": "d", "data": ["help", "me"]}
]

compares = [
    ('Missing Entry',
     [
        {"type": "a", "data":["some", "random"]},
        {"type": "b", "data":["data", "for", "people", "to"]},
        {"type": "b", "data":["mess", "with", "on", "stack"]},
        {"type": "c", "data":["can", "try", "and"]},
        {"type": "d", "data":["help", "me"]}
    ]),
    ('Swapped data',
     [
        {"type": "a", "data":["some", "random"]},
        {"type": "b", "data":["mess", "for", "people", "stack"]},
        {"type": "b", "data":["data", "with", "on", "to"]},
        {"type": "b", "data":["over", "flow", "so", "they"]},
        {"type": "c", "data":["can", "try", "and"]},
        {"type": "d", "data":["help", "me"]}
    ]),
    # ...
        ]

def data_as_textlines(data):
    'Turns a list of dataitems into a list of each items repr string'
    return [repr(item) for item in data]


correct_text = C = data_as_textlines(correct)
for (title, prog_data) in compares:
    print('\n' + title)
    print('=' * len(title))
    prog_text = P = data_as_textlines(prog_data)
    s = SequenceMatcher(None, correct_text, prog_text)
    for tag, i1, i2, j1, j2 in s.get_opcodes():
        print('{:7}   C[{}:{}] --> P[{}:{}] {!r:>8} --> {!r}'.format(
            tag, i1, i2, j1, j2, correct_text[i1:i2], prog_text[j1:j2]))
其输出为:

Missing Entry
=============
equal     C[0:3] --> P[0:3] ["{'type': 'a', 'data': ['some', 'random']}", "{'type': 'b', 'data': ['data', 'for', 'people', 'to']}", "{'type': 'b', 'data': ['mess', 'with', 'on', 'stack']}"] --> ["{'type': 'a', 'data': ['some', 'random']}", "{'type': 'b', 'data': ['data', 'for', 'people', 'to']}", "{'type': 'b', 'data': ['mess', 'with', 'on', 'stack']}"]
delete    C[3:4] --> P[3:3] ["{'type': 'b', 'data': ['over', 'flow', 'so', 'they']}"] --> []
equal     C[4:6] --> P[3:5] ["{'type': 'c', 'data': ['can', 'try', 'and']}", "{'type': 'd', 'data': ['help', 'me']}"] --> ["{'type': 'c', 'data': ['can', 'try', 'and']}", "{'type': 'd', 'data': ['help', 'me']}"]

Swapped data
============
equal     C[0:1] --> P[0:1] ["{'type': 'a', 'data': ['some', 'random']}"] --> ["{'type': 'a', 'data': ['some', 'random']}"]
replace   C[1:3] --> P[1:3] ["{'type': 'b', 'data': ['data', 'for', 'people', 'to']}", "{'type': 'b', 'data': ['mess', 'with', 'on', 'stack']}"] --> ["{'type': 'b', 'data': ['mess', 'for', 'people', 'stack']}", "{'type': 'b', 'data': ['data', 'with', 'on', 'to']}"]
equal     C[3:6] --> P[3:6] ["{'type': 'b', 'data': ['over', 'flow', 'so', 'they']}", "{'type': 'c', 'data': ['can', 'try', 'and']}", "{'type': 'd', 'data': ['help', 'me']}"] --> ["{'type': 'b', 'data': ['over', 'flow', 'so', 'they']}", "{'type': 'c', 'data': ['can', 'try', 'and']}", "{'type': 'd', 'data': ['help', 'me']}"]

查看difflib.SequenceMatcher.get_操作码的文档,您会在它们的示例中发现某些相似之处:-

到目前为止您尝试了什么?提供一些代码?听起来很像Sorry@N.Ivanov忘记了这一点,我本打算把它包括在内。你最终使用了什么?仍在寻找更可行的解决方案。我尝试了difflib,但它仍然不是我真正需要的。