如何使用Python根据一组可变条件过滤列表?
与此相关的问题 我需要帮助根据一组可变条件筛选列表 以下是列表的摘录:如何使用Python根据一组可变条件过滤列表?,python,python-3.x,filtering,Python,Python 3.x,Filtering,与此相关的问题 我需要帮助根据一组可变条件筛选列表 以下是列表的摘录: ex = [ # ["ref", "type", "date"] [1, 'CB', '2017-12-11'], [2, 'CB', '2017-12-01'], [3, 'RET', '2017-11-08'], [1, 'CB', '2017-11-08'], [5, 'RET', '2017-10-10'], ] 我想应用最后一种处理方法,如list(filter(m
ex = [
# ["ref", "type", "date"]
[1, 'CB', '2017-12-11'],
[2, 'CB', '2017-12-01'],
[3, 'RET', '2017-11-08'],
[1, 'CB', '2017-11-08'],
[5, 'RET', '2017-10-10'],
]
我想应用最后一种处理方法,如list(filter(makeCondition,ex))
wheremakeCondition(myList,**kwargs)
函数返回一个布尔值,用于过滤列表
我很难按照本文的建议构建此函数,因为**kwargs词汇表中定义的条件数量是可变的
条件集示例:
conditions = {"ref": 3}
conditions = {"ref": 1, "type": "CB"}
这是一个开始:
def makeConditions(myList, **p):
# For each key:value in the dictionnary
for key, value in p.items():
if key == "ref":
lambda x: x[0] == value
elif key == "type":
lambda x: x[1] == value
elif key == "date":
lambda x: x[2] == value
# return chained conditions...
list(filter(makeConditions, ex))
我不明白各种评论试图在上面提到的帖子中给出的逻辑观点。。。我是否必须为每个子列表执行
filter()
函数,还是可以为整个全局列表执行该函数?欢迎提供任何建议 您就快到了,我们的想法是创建一个包含检查所需条件的函数的列表,一旦您有了这些函数,您就可以在需要检查的列表上调用这些函数,并使用all
函数检查是否所有函数都被计算为True
,注意使用了partial,因此过滤器
调用中的函数仅获取数据列表;选中此项:
from functools import partial
ex = [
# ["ref", "type", "date"]
[1, 'CB', '2017-12-11'],
[2, 'CB', '2017-12-01'],
[3, 'RET', '2017-11-08'],
[1, 'CB', '2017-11-08'],
[5, 'RET', '2017-10-10'],
]
conditions = {"ref": 3}
conditions2 = {"ref": 1, "type": "CB"}
def apply(data, *args):
"""
same as map, but takes some data and a variable list of functions instead
it will make all that functions evaluate over that data
"""
return map(lambda f: f(data), args)
def makeConditions(p, myList):
# For each key:value in the dictionnary
def checkvalue(index, val, lst):
return lst[index] == val
conds = []
for key, value in p.items():
if key == "ref":
conds.append(partial(checkvalue, 0, value))
elif key == "type":
conds.append(partial(checkvalue, 1, value))
elif key == "date":
conds.append(partial(checkvalue, 2, value))
return all(apply(myList, *conds)) # does all the value checks evaluate to true?
#use partial to bind the conditions to the makeConditions function
print(list(filter(partial(makeConditions, conditions), ex)))
#[[3, 'RET', '2017-11-08']]
print(list(filter(partial(makeConditions, conditions2), ex)))
#[[1, 'CB', '2017-12-11'], [1, 'CB', '2017-11-08']]
给你一个
是否必须为每个子列表执行filter()函数,或者是否可以为整个全局列表执行该函数
Filter对所有列表进行迭代,为每个元素使用一个函数,如果该函数的计算结果为True,则元素将在结果中提醒,因此,Filter对整个全局列表起作用我只需创建一个返回条件的函数:
def makeConditions(**p):
fieldname = {"ref": 0, "type": 1, "date": 2 }
def filterfunc(elt):
for k, v in p.items():
if elt[fieldname[k]] != v: # if one condition is not met: false
return False
return True
return filterfunc
然后您可以这样使用它:
>>> list(filter(makeConditions(ref=1), ex))
[[1, 'CB', '2017-12-11'], [1, 'CB', '2017-11-08']]
>>> list(filter(makeConditions(type='CB'), ex))
[[1, 'CB', '2017-12-11'], [2, 'CB', '2017-12-01'], [1, 'CB', '2017-11-08']]
>>> list(filter(makeConditions(type='CB', ref=2), ex))
[[2, 'CB', '2017-12-01']]
它总是有ref、type和date,还是可变的?
条件
变量确实是可变的,如文章所示。子列表的内容始终与3个对象保持一致:ref、type和date。非常感谢@Daniel Sanchez!你的建议给我带来了一些新的东西,总之效果很好。我必须阅读有关partial()
函数的文档。我不知道checkvalue()
函数如何找到索引
参数。@wiltomap,partial将值绑定到返回另一个函数的函数参数,这些参数用这些值固定,因此当我调用partial(checkvalue,0,value)
时,我正在创建一个与lambda相同的函数,只是以后用。它看起来不错,而且短得多。。。谢谢@Serge Ballesta!我会仔细看看,然后回来。