我如何从python中的对象访问原始数据。在这里,我使用分组依据对数据进行分组

问题描述 投票:0回答:1

我试图根据我拥有的以下数据字段以及当我无法访问这些字段中的原始数据时对数据进行分组

打印filtered_data给出类似“对象位于0x10dd1abf0>”的内容,因此我需要访问对象中原始的人类可读值。

    data_objects = ['*', '*', '*', ......] // This is list of data items
    filterd_data_objects = groupby(
        data_objects, lambda data: (data.x, data.y, data.z) and data.p
    )
    print(filtered_data_objects)
    // This is giving <itertools.groupby object at 0x1066ceb30>, Need to access the original content in the data objects.
    for filterd_data_object, _ in filterd_data_objects:
        x = data_object[0] // this is not working I've tried this to access the original data
        y = data_object[1]
        z = data_object[2]
        p = data_object[3]
django python-3.x object django-rest-framework itertools
1个回答
0
投票

您需要通过列表将data_object包裹起来,例如

list(data_object)

您可以参考此示例

from itertools import groupby
data_objects  = [{"a": 1}, {"a": 1}, {"a": 2}]

for dobject, x in groupby( data_objects, lambda data : data["a"]):
    print(dobject,list(x))
© www.soinside.com 2019 - 2024. All rights reserved.