使用另一个包含多个项的词典筛选词典

2024-09-28 14:18:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个字典,我想用另一个字典过滤('过滤选项'显示大约一半了这篇文章)与多个项目。我可以为一个条目而不是整个词典找到答案…我已经找了一段时间了,但是其他的答案只针对一个条目的过滤器(通过词典理解很好地解决了这个问题)

这是我到目前为止所做的单项字典过滤器,即

filter_options = {
    'Attack':   25}

for kfo, vfo in filter_options.iteritems():
    for kp, vp in pokers.iteritems():
       if vp[kfo] >= vfo:
           print pokedex[kp]

它的工作,但我不能找出它来过滤多个项目

这是这本词典的删节版本

pokedex = {1: {'Attack': 49.0,
  'Defense': 49.0,
  'HP': 45.0,
  'Name': 'Bulbasaur',
  'PokedexNumber': 1.0,
  'SpecialAttack': 65.0,
  'SpecialDefense': 65.0,
  'Speed': 45.0,
  'Total': 318.0,
  'Type': 'GrassPoison'},
 2: {'Attack': 62.0,
  'Defense': 63.0,
  'HP': 60.0,
  'Name': 'Ivysaur',
  'PokedexNumber': 2.0,
  'SpecialAttack': 80.0,
  'SpecialDefense': 80.0,
  'Speed': 60.0,
  'Total': 405.0,
  'Type': 'GrassPoison'},
 3: {'Attack': 82.0,
  'Defense': 83.0,
  'HP': 80.0,
  'Name': 'Venusaur',
  'PokedexNumber': 3.0,
  'SpecialAttack': 100.0,
  'SpecialDefense': 100.0,
  'Speed': 80.0,
  'Total': 525.0,
  'Type': 'GrassPoison'}}

# Only filter based on parameters passed

    filter_options = {
        'Attack':   25,
        'Defense':  30,
        'Type':     'Electric'
        }

例如,返回攻击大于等于25,防御大于等于30,类型大于等于“电气”的记录 还可以预期其他参数也可以传递,如“specialatack”、“Speed”等

输出示例:

[{'Attack': 30.0,
'Defense': 50.0,
'HP': 40.0,
'Name': 'Voltorb',
'SpecialAttack': 55.0,
'SpecialDefense': 55.0,
'Speed': 100.0,
'Total': 330.0,
'Type': 'Electric'},
{'Attack': 30.0,
'Defense': 33.0,
'HP': 32.0,
'Name': 'Pikachu',
'SpecialAttack': 55.0,
'SpecialDefense': 55.0,
'Speed': 100.0,
'Total': 330.0,
'Type': 'Electric'},
... etc
]

我将沿着

def filtered_pokedex(pokedex_data, filter=filter_options):
....etc

但我能自己解决

如果它需要更好的解释或编辑只是让我知道干杯…有史以来第一次在堆栈交换问题,所以希望我提供了足够的信息

干杯


Tags: name字典typefilterhpoptionstotalspeed
3条回答

在这个场景中使用all。检查值是数字类型还是字符串类型,并相应地更改条件。你知道吗

def foo(vp, k, v):
    return vp[k] > v if isinstance(v, (int, float)) else vp[k] == v

for kp, vp in pokedex.iteritems():
    if all(foo(vp, k, v) for k, v in filter_options.iteritems()):
        print vp

我定义了一个函数foo来处理检查,因为它会整理代码。你知道吗

用Python回答您的问题:递归地过滤每个选项的“单一过滤”结果,直到生成结果。为了使单个过滤器的大小写更合适,过滤器选项被重新组织以包含更多信息。你知道吗

但是当允许不同的操作类型时,它变得复杂了。这个问题并没有明确地提出这个问题,但需要超越第一个例子。允许在一组过滤器中使用多种操作类型的最简单的解决方案是一个类似于“开关”的构造,其中包含每个可能操作的函数,但“更好”的解决方案是从标准操作符库中传递操作符本身。你知道吗

# pokedex = ...

filter_options = [
    {
        'attribute': 'Attack',
        'operator': '>=',
        'value': 25,
    },
    {
        'attribute': 'Defense',
        'operator': '>=',
        'value': 30,
    },
    {
        'attribute': 'Type',
        'operator': '==',
        'value': 'Electric',
    },
]

# Better to use: https://docs.python.org/2/library/operator.html
operators = {
    '<': lambda a, b: a < b,
    '>': lambda a, b: a > b,
    '==': lambda a, b: a == b,
    '<=': lambda a, b: a <= b,
    '>=': lambda a, b: a >= b,
}


def filter_single(attribute, operator, value, pokedex=pokedex):
    result = {}
    for number, pokemon in pokedex.iteritems():
        if operators[operator](pokemon[attribute], value):
            result[number] = pokemon
    return result


def filter(filter_options, pokedex=pokedex):
    result = filter_single(
        filter_options[0]['attribute'],
        filter_options[0]['operator'],
        filter_options[0]['value'],
        pokedex,
    )
    for option in filter_options[1:]:
        result = filter_single(
            option['attribute'],
            option['operator'],
            option['value'],
            result,
        )
    return result


print filter(filter_options)

这段代码是用Python3测试的,但应该可以用2.7。将print替换为print(),将.iteritems()替换为.items()以转换为Python3。你知道吗


对于结构化查询语言(SQL),这种类型的查询很容易考虑。连接数据结构和思维模式是SQL的目的之一。你知道吗

示例:

SELECT * FROM pokedex
WHERE attack >= 25
  AND defense >= 30
  AND type == 'Electric';

另外,我认为问题的描述缺少了“pokers”变量似乎是所有pokemon可用的属性,但是如果假设过滤器选项总是有效的属性名,则不需要这样做。使用FilterOption类是强制执行有效筛选器的一种方法。你知道吗

这里有一个解决方案:

import pandas as pd

df = pd.DataFrame(pokedex).T

df # change last entry to Type = "Electric" for demo output.

  Attack Defense  HP       Name   ...         Type
1     49      49  45  Bulbasaur   ...  GrassPoison
2     62      63  60    Ivysaur   ...  GrassPoison                    
3     82      83  80   Venusaur   ...     Electric                    

现在基于filter_options构建一个布尔掩码:

mask = [True] * len(df)

for key in filter_options:
    if isinstance(filter_options[key], int):
        mask = mask & (df[key] >= filter_options[key]).values
    elif isinstance(filter_options[key], str):
        mask = mask & (df[key] == filter_options[key]).values
    else:
        continue

df.loc[mask]

  Attack Defense  HP      Name  ...     Type
3     82      83  80  Venusaur  ... Electric     

相关问题 更多 >