我如何显示此json私有给字段用户
我不想这样做:
result = [x['date']['nom']['count'] for x in hits]
{'date': '04-04-2019', 'nom': 'Iphone', 'count': 0, 'users': [1]}
答案 0 :(得分:0)
看起来像你想要的是
result = [{k:v for k, v in entry.items() if k != 'users'} for entry in hits]
更一般的情况可能是:
def filter_dict(d: Mapping[String, Any],
blacklist: Optional[List[String]]=None) -> Mapping[String, Any]:
if blacklist is None:
return d
return {k:v for k, v in d.items() if k not in blacklist}
result = [filter_dict(entry, blacklist=['users']) for entry in hits]
答案 1 :(得分:0)
我找到了解决方案:
def removekey(d, key):
r = dict(d)
del r[key]
return r
texte = [removekey(x['_source'], 'users') for x in hits]