我有以下python代码:
result = [{k:str(row[k]) for k in row.keys()} for row in rows]
现在我想添加这样的条件: if k != 'count': add to the list else: do not add to list。
如果您迭代项目,您将有更少的查找。假设所有键都是字符串,这if
很容易:
result = [{k:str(v) for k,v in row.items() if k != 'count'} for row in rows]
...或者如果你只想添加没有计数的东西
result = [row.copy() for row in rows if 'count' not in row]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句