拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 Python:如果我有一个dicts{person_id,list,account}串列,如何洗掉重复的person_id并组合串列值?

Python:如果我有一个dicts{person_id,list,account}串列,如何洗掉重复的person_id并组合串列值?

白鹭 - 2022-03-11 2298 0 0

这是要点,我使用 Django 来填充 PostgreSQL 数据库,以存盘来自第三方 API 的用户资料。我正在使用 API 将资料汇入 Django,以便我可以自动填充数据库。我为需要存盘的栏位构建了模型。

这是我需要帮助的地方。我已经从 API 回应创建了一个串列,但我想洗掉重复的用户并合并这些串列,就像这样。

What I have now.
    {
        "person_id": "1",
        "account": "5",
        "list": "c"
    },
    {
        "person_id": "1",
        "account": "5",
        "list": "b"
    },
    {
        "person_id": "1",
        "account": "5",
        "list": "a"
    },
...
What i want
    {
        "person_id": "1",
        "account": "5",
        "list": ["a","b","c"]
    },
    {
        "person_id": "2",
        "account": "5",
        "list": ["a","c"]
    },
    {
        "person_id": "3",
        "account": "5"
        "list": ["a","b"]
    },
...

我正在进行的一个 API 呼叫是将所有用户都放在一个串列中并回复:

API RESPONSE
{
    "records": [
        {
            "id": "asdafdgsdfhsdfh",
            "email": "email@email.com",
            "phone_number": " 1123123"
        },
        {
            "id": "asdafdgsdfhsdfh",
            "email": "email@email.com",
            "phone_number": " 1123123"
        },
       ...
 ],
 "marker":342523452
}

根据该回应,我正在迭代每条记录并创建一个 dict 以添加到串列中。

 def personA():
        return dict(
            person_id = record["id"],
            account = account,
            list = list   

r = requests.request('GET',f"{link}")
        resdata =r.json()
        for r in resdata:
            for record in resdata["records"]:
                listC = personA()
                listData.append(listC)         
    ) 

我正在为账户中的每个串列执行此操作,因此某些“person_id”出现多次,而某些仅出现一次。

以我想要的方式创建串列的最佳方式是什么?

uj5u.com热心网友回复:

您可以创建人员到他们出现的串列的映射:

from collections import defaultdict

separate_data = [
    {
        "person_id": "1",
        "account": "5",
        "list": "c"
    },
    {
        "person_id": "1",
        "account": "5",
        "list": "b"
    },
    {
        "person_id": "1",
        "account": "5",
        "list": "a"
    },
]
merged = defaultdict(list)
for record in separate_data:
    key = record["person_id"], record["account"]
    value = record["list"]
    merged[key].append(value)

results = [
    {
        "person_id": person_id,
        "account": account,
        "list": sorted(lists)
    } for (person_id, account), lists in merged.items()
]

[{'account': '5', 'list': ['a', 'b', 'c'], 'person_id': '1'}]

uj5u.com热心网友回复:

字典将在这里完成:

# Grab the account_id from the first element in data. I'm assuming that the account names are the same across all data points, but this is not a serious concern per the comment.
account_id = data[0]["account"]
# We maintain a dictionary that maps from a person_id to a list of strings appearing in the list field for a given person_id.
person_id_elements = {}

# Read each data point into our dictionary.
for data_point in data:
    person_id = data_point["person_id"]
    list_element = data_point["list"]
    if person_id not in person_id_elements:
        person_id_elements[person_id] = []
    
    person_id_elements[person_id].append(list_element)

# Transform id_data into objects that observe the desired schema.
result = []
for person_id in person_id_elements:
    result.append({
        "person_id": person_id,
        "account": account_id,
        "list": sorted(person_id_elements[person_id]) # Sorted, as shown in the sample output.
    })

print(result)
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *