社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python方法1变量嵌套字典

Eran Moshe • 6 年前 • 1871 次点击  

我发现这类问题很难命名,但肯定是个简单的问题,我遗漏了一些基本的东西。

假设我有以下python dict():

import json

dct = dict()
dct['hits'] = dict()
dct['hits']['hits'] = dict()
dct['hits']['hits']['a'] = 'b'
dct['hits']['hits']['b'] = 'c'
dct['aggregations'] = dict()
dct['aggregations']['a'] = 1
dct['aggregations']['b'] = 2

print(json.dumps(dct, indent=2))

{
  "hits": {
    "hits": {
      "a": "b",
      "b": "c"
    }
  },
  "aggregations": {
    "a": 1,
    "b": 2
  }
}

这看起来可能很熟悉,因为它是elasticsearch返回结果的结构。

我正在构建一个使用该结果的函数。但有时我想接近 dct['hits']['hits'] 有时我想接近 dct['aggregations'] .

自然地,我会使用一个带有变量的函数来建议我要访问哪个字段,如下所示:

def foo(field):
    return dct[field]

如果 field='aggregations' 一切都很好。但如果我想让这个领域 ['hits']['hits'] ?


一种解决方法(但很难看),迭代方法:

def foo(fields=('hits','hits')):
    wanted = dct
    for field in fields:
        wanted = wanted[field]
    return wanted

a = foo()
a
Out[47]: {'a': 'b', 'b': 'c'}
a = foo(('aggregations',))
a
Out[51]: {'a': 1, 'b': 2}

我试图修改的实际函数:

def execute_scroll_query(es_client, query, indexes):
    try:
        response = es_client.search(index=indexes, scroll='2m', size=1000, body=query)
        scroll_size = len(response['hits']['hits'])
        sid = response['_scroll_id']
        while scroll_size > 0:
            try:
                for hit in response['hits']['hits']:
                    yield hit
                response = es_client.scroll(scroll_id=sid, scroll='2m')
                sid = response['_scroll_id']
                scroll_size = len(response['hits']['hits'])
            except Exception:
                print("Unexpected Exception while scrolling")
    except Exception:
        print("Unexpected Exception while fetching")
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40817
 
1871 次点击  
文章 [ 3 ]  |  最新文章 6 年前