我有一个字典,我想用另一个字典过滤('过滤选项'显示大约一半了这篇文章)与多个项目。我可以为一个条目而不是整个词典找到答案…我已经找了一段时间了,但是其他的答案只针对一个条目的过滤器(通过词典理解很好地解决了这个问题)
这是我到目前为止所做的单项字典过滤器,即
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
但我能自己解决
如果它需要更好的解释或编辑只是让我知道干杯…有史以来第一次在堆栈交换问题,所以希望我提供了足够的信息
干杯
在这个场景中使用
all
。检查值是数字类型还是字符串类型,并相应地更改条件。你知道吗我定义了一个函数
foo
来处理检查,因为它会整理代码。你知道吗用Python回答您的问题:递归地过滤每个选项的“单一过滤”结果,直到生成结果。为了使单个过滤器的大小写更合适,过滤器选项被重新组织以包含更多信息。你知道吗
但是当允许不同的操作类型时,它变得复杂了。这个问题并没有明确地提出这个问题,但需要超越第一个例子。允许在一组过滤器中使用多种操作类型的最简单的解决方案是一个类似于“开关”的构造,其中包含每个可能操作的函数,但“更好”的解决方案是从标准操作符库中传递操作符本身。你知道吗
这段代码是用Python3测试的,但应该可以用2.7。将
print
替换为print()
,将.iteritems()
替换为.items()
以转换为Python3。你知道吗对于结构化查询语言(SQL),这种类型的查询很容易考虑。连接数据结构和思维模式是SQL的目的之一。你知道吗
示例:
另外,我认为问题的描述缺少了“pokers”变量似乎是所有pokemon可用的属性,但是如果假设过滤器选项总是有效的属性名,则不需要这样做。使用FilterOption类是强制执行有效筛选器的一种方法。你知道吗
这里有一个解决方案:
现在基于
filter_options
构建一个布尔掩码:相关问题 更多 >
编程相关推荐