从数组中过滤数据的Python函数

问题描述 投票:0回答:1

我已经制作了循环数组并过滤结果的函数。

def filter_data(data, match_d=None, trim_matches=False):
    result_matches = {}  # stores matched
    result_unmatches = {}  # stores not matched

    # loop every record in data
    for key, value in data.items():

        # match record with filter criteria
        for search_string, search_key_name in match_d.items():

            if search_string == value[search_key_name]:
                result_matches[key] = value
            else:
                result_unmatches[key] = value

    if trim_matches is False:  # return only matches
        return result_matches
    else:  # return all but matches
        return result_unmatches



data_dict = {0: {'name': 'one'},
             1: {'name': 'two'},
             2: {'name': 'three'},
             3: {'name': 'four'},
             4: {'name': 'five'}}

result = filter_data(data_dict,
                     match_d={'one': 'name', 'two': 'name'},
                     trim_matches=True)

print(result)

#returns: {0: {'name': 'one'}, 1: {'name': 'two'}, 2: {'name': 'three'}, 3: {'name': 'four'}, 4: {'name': 'five'}}

#expected {2: {'name': 'three'}, 3: {'name': 'four'}, 4: {'name': 'five'}}

当我在函数调用中使用

trim_matches=False
时,它会按预期工作,但是当我使用
trim_matches=True
时,它不会。

原因是它在每场比赛中循环关键槽,然后记录在两个列表中

result_matches
result_unmatches
,但是我找不到简单的解决方案。

我可以简单地写

result_matches
然后比较
data
并在使用
trim_matches=True
时从数据中删除匹配项,但我不想添加更多代码来运行。

我正在寻找的解决方案是让当前的函数以尽可能少的额外代码工作。换句话说,我希望这个逻辑能够工作,而不是向这个函数添加更多行代码并以任何方式更改它。

替代解决方案重写完整的功能代码和逻辑,但应该比当前使用的代码少。

不允许仅包含纯Python。

编辑:意识到调试很困难,我继续对输出进行着色和间隔,现在很容易看到发生了什么。

python loops filter
1个回答
0
投票

抽象循环可以说明为什么它不起作用。

for i in (1, 2, 3):
    for j in (1, 2):
        if i == j:
            print('match', i)
        else:
            print('no match', i)

match 1
no match 1
no match 2
match 2
no match 3
no match 3

对于每场比赛,您都无法匹配所有其他数字,因此当您此时添加到字典中时,您将添加所有内容。

将其更改为 for/else 循环是确定何时没有数字匹配的最佳方法。

for i in (1, 2, 3):
    for j in (1, 2):
        if i == j:
            print('match', i)
            break
    else:
        print('no match', i)

match 1
match 2
no match 3

你的函数就变成这样了:

def filter_data(data, match_d=None, trim_matches=False):
    result_matches = {}  # stores matched
    result_unmatches = {}  # stores not matched

    # loop every record in data
    for key, value in data.items():

        # match record with filter criteria
        for search_string, search_key_name in match_d.items():

            if search_string == value[search_key_name]:
                result_matches[key] = value
                break
        else:
            result_unmatches[key] = value

    if trim_matches:
        return result_unmatches
    return result_matches

# match: {0: {'name': 'one'}, 1: {'name': 'two'}}
# no match: {2: {'name': 'three'}, 3: {'name': 'four'}, 4: {'name': 'five'}}

请注意,如果您的数据字典包含重复名称的项目,则 for/else 循环将不起作用。您可以通过设置一个变量来确定是否发生任何匹配来完成此操作。

for i in (1, 2, 3):
    matched = False
    for j in (1, 1):
        if i == j:
           matched = True
           print('match', i)
    if not matched:
        print('no match', i)

match 1
match 1
no match 2
no match 3
© www.soinside.com 2019 - 2024. All rights reserved.