合并两个可能不相等的列表,结构数量较少

时间:2017-11-28 16:02:42

标签: python algorithm python-3.x list merge

我正在努力解决一个相当棘手的问题。 我想计算两个列表之间的某种差异度量。一个是验证正确的数据,另一个是由程序生成的。 我想测试这个程序的准确程度,但为了达到这个目的,我需要以某种方式合并这两个列表。

示例数据可以在下面找到。

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

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

  • {"matching": True}
  • {"matching": False, "error_type": "deleted"}
  • {"matching": False, "error_type": "inserted"}
  • {"matching": False, "error_type": "updated"}

我在Python 3中工作,所以如果有人能够提供令人惊叹的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...

我最初正在寻找一些通用的&#34;合并两个具有类似结构的不平等列表&#34;可以应用于此的算法,但我无法找到实现此目的的算法。

正确的数据:

[
    {"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"]}
]

该程序可以生成各种错误数据,这里有几个例子:

  • 缺少参赛作品:

    [
        {"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},
    ]
    
  • 交换数据:

    [
        {"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": False, "error_type": "updated"},
        {"matching": False, "error_type": "updated"},
        {"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": False, "error_type": "deleted"},
        {"matching": True},
        {"matching": True},
        {"matching": False, "error_type": "deleted"},
        {"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": False, "error_type": "deleted"},
        {"matching": True},
        {"matching": False, "error_type": "deleted"},
        {"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": False, "error_type": "updated"},
        {"matching": False, "error_type": "inserted"},
        {"matching": True},
        {"matching": True},
        {"matching": True}
    ]
    

1 个答案:

答案 0 :(得分:1)

你似乎在分歧。差异化并不容易。

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_opcodes的文档您将在他们的示例中找到某些相似之处: - )