我最近发现自己编写了这行代码,我不太喜欢这样做:
if ('upload' in request.POST) or ('delete' in request.POST):
我已经考虑过列表理解,看起来像这样:
if [value for value in ['upload','delete'] if value in request.POST]:
哪个不是更好。我非常简单的问题是:这可以简化吗?或者这只是想要太聪明?
答案 0 :(得分:6)
您可以使用set intersection:
更简洁地编写它if {'upload', 'delete'} & set(request.POST):
或更明确地说:
if {'upload', 'delete'}.intersection(request.POST):
答案 1 :(得分:1)
简化使用any()
。
if any(value for value in ['upload','delete'] if value in request.POST):